Shiro 提供了JSP
的一套JSTL
标签,用于做JSP
页面做权限控制的。可以控制一些按钮和一些超链接,或者一些显示内容。JSP
的下篇文章会讲到,这篇先讲讲 Freemarker 的 Shiro 标签。
引用包:
<!-- freemarker + shiro(标签) begin -->
<dependency>
<groupId>net.mingsoft</groupId>
<artifactId>shiro-freemarker-tags</artifactId>
<version>0.1</version>
</dependency>
<!-- freemarker + shiro(标签) begin -->
他不是 shiro 官方提供的。如果不是 Maven 项目,请在下方附件内提取。
Java代码:
public class FreeMarkerConfigExtend extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
cfg.setSharedVariable("shiro", new ShiroTags());//shiro标签
cfg.setNumberFormat("#");//防止页面输出数字,变成2,000
//可以添加很多自己的要传输到页面的[方法、对象、值]
}
}
配置文件:
<!-- 配置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>
下面来介绍标签:
1.guest(游客)
<@shiro.guest>
您当前是游客,<a href="javascript:void(0);" class="dropdown-toggle qqlogin" >登录</a>
</@shiro.guest>
2.user(已经登录,或者记住我登录)
<@shiro.user>
欢迎[<@shiro.principal/>]登录,<a href="/logout.shtml">退出</a>
</@shiro.user>
3.authenticated(已经认证,排除记住我登录的)
<@shiro.authenticated>
用户[<@shiro.principal/>]已身份验证通过
</@shiro.authenticated>
4.notAuthenticated(和authenticated相反)
<@shiro.notAuthenticated>
当前身份未认证(包括记住我登录的)
</@shiro.notAuthenticated>
这个功能主要用途,识别是不是本次操作登录过的,比如支付系统,进入系统可以用记住我的登录信息,但是当要关键操作的时候,需要进行认证识别。
5.principal标签,这个要稍微重点讲讲。好多博客都是一下带过。
principal标签,取值取的是你登录的时候。在Realm实现类中的如下代码:
....
return new SimpleAuthenticationInfo(user,user.getPswd(), getName());
在new SimpleAuthenticationInfo(第一个参数,....)的第一个参数放的如果是一个username,那么就可以直接用。
<!--取到username-->
<@shiro. principal/>
如果第一个参数放的是对象,比如我喜欢放User对象。那么如果要取username字段。
<!--需要指定property-->
<@shiro.principal property="username"/>
和Java如下Java代码一致
User user = (User)SecurityUtils.getSubject().getPrincipals();
String username = user.getUsername();
6.hasRole标签(判断是否拥有这个角色)
<@shiro.hasRole name="admin">
用户[<@shiro.principal/>]拥有角色admin<br/>
</@shiro.hasRole>
7.hasAnyRoles标签(判断是否拥有这些角色的其中一个)
<@shiro.hasAnyRoles name="admin,user,member">
用户[<@shiro.principal/>]拥有角色admin或user或member<br/>
</@shiro.hasAnyRoles>
8.lacksRole标签(判断是否不拥有这个角色)
<@shiro.lacksRole name="admin">
用户[<@shiro.principal/>]不拥有admin角色
</@shiro.lacksRole>
9.hasPermission标签(判断是否有拥有这个权限)
<@shiro.hasPermission name="user:add">
用户[<@shiro.principal/>]拥有user:add权限
</@shiro.hasPermission>
10.lacksPermission标签(判断是否没有这个权限)
<@shiro.lacksPermission name="user:add">
用户[<@shiro.principal/>]不拥有user:add权限
</@shiro.lacksPermission>