Shiro提供了JSP的一套JSTL标签,用于做JSP页面做权限控制的。可以控制一些按钮和一些超链接,或者一些显示内容。这篇先讲讲 Freemarker的 Shiro标签。
开始使用
引用包:
1 | <!-- freemarker + shiro(标签) begin --> |
- 他不是 shiro 官方提供的。如果不是 Maven 项目,请先下载。
Java代码:
1
2
3
4
5
6
7
8
9
10public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
cfg.setSharedVariable("shiro", new ShiroTags());//shiro标签
cfg.setNumberFormat("#");//防止页面输出数字,变成2,000
//可以添加很多自己的要传输到页面的[方法、对象、值]
}
}配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38<!-- 配置freeMarker 拓展-->
<bean id="freemarkerConfig" class="com.sojson.core.freemarker.extend.FreeMarkerConfigExtend">
<property name="templateLoaderPath">
<value>/WEB-INF/ftl/</value>
</property>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape"/>
<entry key="api" value-ref="api"/>
</map>
</property>
<property name="defaultEncoding">
<value>utf-8</value>
</property>
<property name="freemarkerSettings">
<props>
<!-- 315360000 -->
<prop key="template_update_delay">0</prop>
<prop key="defaultEncoding">UTF-8</prop>
<prop key="url_escaping_charset">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<prop key="boolean_format">true,false</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">0.######</prop>-->
<prop key="number_format">#</prop>
<prop key="whitespace_stripping">true</prop>
<prop key="auto_import">
/common/config/top.ftl as _top,
/common/config/left.ftl as _left,
/common/config/html.ftl as _html,
/common/config/bottom.ftl as _footer,
/common/config/menu.ftl as _menu
</prop>
</props>
</property>
</bean>下面来介绍标签:
- guest(游客)
1
2
3<@shiro.guest>
您当前是游客,<a href="javascript:void(0);" class="dropdown-toggle qqlogin" >登录</a>
</@shiro.guest>
2.user(已经登录,或者记住我登录)
1 | <@shiro.user> |
3.authenticated(已经认证,排除记住我登录的)
1 | <@shiro.authenticated> |
- notAuthenticated(和authenticated相反)
1
2
3<@shiro.notAuthenticated>
当前身份未认证(包括记住我登录的)
</@shiro.notAuthenticated>
- 这个功能主要用途,识别是不是本次操作登录过的,比如支付系统,进入系统可以用记住我的登录信息,但是当要关键操作的时候,需要进行认证识别。
- principal标签,这个要稍微重点讲讲。好多博客都是一下带过。
principal
标签,取值取的是你登录的时候。在Realm
实现类中的如下代码:return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
- 在new SimpleAuthenticationInfo(第一个参数,….)的第一个参数放的如果是一个username,那么就可以直接用。
<@shiro. principal/>
如果第一个参数放的是对象,比如我喜欢放User对象。那么如果要取username字段。<@shiro.principal property="username"/>
和Java如下Java代码一致1
2User user = (User)SecurityUtils.getSubject().getPrincipals();
String username = user.getUsername();
- hasRole标签(判断是否拥有这个角色)
1
2
3<@shiro.hasRole name="admin">
用户[<@shiro.principal/>]拥有角色admin<br/>
</@shiro.hasRole> - hasAnyRoles标签(判断是否拥有这些角色的其中一个)
1
2
3<@shiro.hasAnyRoles name="admin,user,member">
用户[<@shiro.principal/>]拥有角色admin或user或member<br/>
</@shiro.hasAnyRoles> - lacksRole标签(判断是否不拥有这个角色)
1
2
3<@shiro.lacksRole name="admin">
用户[<@shiro.principal/>]不拥有admin角色
</@shiro.lacksRole> - hasPermission标签(判断是否有拥有这个权限)
1
2
3<@shiro.hasPermission name="user:add">
用户[<@shiro.principal/>]拥有user:add权限
</@shiro.hasPermission> - lacksPermission标签(判断是否没有这个权限)
1
2
3<@shiro.lacksPermission name="user:add">
用户[<@shiro.principal/>]不拥有user:add权限
</@shiro.lacksPermission> - 自定义标签。
资料
http://www.sojson.com/blog/143.html
shiro-freemarker-tags jar: http://mvnrepository.com/artifact/net.mingsoft/shiro-freemarker-tags/0.1