其实标签都是和Freemarker Shiro标签对应的,只不过是把@shiro.xxx 改成shiro:xxx 。下面来细细道来。
不过引入有区分,JSP需要在头部引入, Shiro 提供的TLD 文件:
//必须 <%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %> <shiro:guest> 您当前是游客,<a href="java: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 代码一致 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>
关注"都市百货" 了解南陵
微信咨询wanglf2r(不拉群 发广告者勿加)
热门评论