Apache Shiro学习----授权

Shiro支持三种方式的授权:

1.编程式:通过写if/else授权代码块完成: 

Subject subject = SecurityUtils.getSubject();  
if(subject.hasRole(“admin”)) {  
    //有权限  
} else {  
    //无权限  
} 

2.注解式:通过在执行的Java方法上放置相应的注解完成: 

@RequiresRoles("admin")  
public void hello() {  
    //有权限  
}  

3.JSP/GSP标签:在JSP/GSP页面通过相应的标签完成: 

<shiro:hasRole name="admin">  
<!— 有权限 —>  
</shiro:hasRole>  

这里我们重点讲下第三种方式---jsp标签

1.了解一些标签的定义

 <shiro:guest>欢迎游客访问,<a href="${pageContext.request.contextPath}/login.jsp">登录</a>  </shiro:guest>  用户没有身份验证时显示相应信息,即游客访问信息。

 <shiro:principal type="java.lang.String"  property="username"/>  显示用户身份信息

 <shiro:user>欢迎[<shiro:principal/>]登录,<a href="${pageContext.request.contextPath}/logout">退出</a>  </shiro:user>  用户已经身份验证/记住我登录后显示相应的信息。

<shiro:hasRole name="admin">     用户拥有角色admin </shiro:hasRole>   

<shiro:hasAnyRoles name="admin,user">    用户拥有角色admin或user  </shiro:hasAnyRoles>   

<shiro:lacksRole name="abc">    用户没有角色abc </shiro:lacksRole>  

<shiro:hasPermission name="user:create">    用户拥有权限user:create </shiro:hasPermission> 

<shiro:lacksPermission name="org:create">      用户没有权限org:create  </shiro:lacksPermission>  

2.想使用Shiro标签首先引用标签库<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>

原文地址:https://www.cnblogs.com/mcahkf/p/7238407.html