当我们需要将页面中一些没有权限的标签给隐藏掉的时候,shiro默认提供了对jsp的支持,但是我们有些时候前端模板引擎使用的是freemarker,这时我们就需要专门来设置下了。
<dependency>
<groupId>net.mingsoft</groupId>
<artifactId>shiro-freemarker-tags</artifactId>
<version>0.1</version>
</dependency>
/**
* 自定义FreemarkerConfigurer类
* @author dengp
*
*/
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
// TODO Auto-generated method stub
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
//shiro标签
cfg.setSharedVariable("shiro", new ShiroTags());
}
}
<!-- 2.定义模板属性 -->
<bean
class="com.sxt.freemarker.FreeMarkerConfigExtend">
<!-- 定义模板位置 -->
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<!-- 编码方式 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 设置键值对 -->
<property name="freemarkerVariables">
<map>
<entry key="root" value="${root}"></entry>
<entry key="a1" value="${a1}"></entry>
</map>
</property>
<!--设置属性值 -->
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="number_format">#.####</prop>
</props>
</property>
</bean>
在需要使用的ftl页面中如下使用
<@shiro.hasRole name="role1">
用户[<@shiro.principal/>]拥有角色role1<br/>
</@shiro.hasRole>
更多shiro标签使用参考Shiro教程7(整合SSM项目-授权)