spring-shiro.xml
| 
1 
2 
3 | /admin/repairType/index
 = roles["ROLE_ADMIN"]/admin/user=roles["ROLE_ADMIN"]/admin/complaint/list=
 roles["ROLE_SERVICE,ROLE_ADMIN"] | 
jsp页面:
| 
1 
2 
3 
4 
5 
6 
7 
8 
9 | <shiro:hasRolename="ROLE_ADMIN">    <liclass="user"><ahref="${ctx}/admin/user">用户</a></li></shiro:hasRole><shiro:hasAnyRolesname="ROLE_ADMIN,ROLE_SERVICE">    <liclass="complaint"><ahref="${ctx}/admin/complaint/list">服务</a></li></shiro:hasAnyRoles><shiro:hasRolename="ROLE_ADMIN">    <liclass="system"><ahref="${ctx}/admin/repairType/index">系统设置</a></li></shiro:hasRole> | 
在使用Shiro标签库前,首先需要在JSP引入shiro标签:
| 
1 |  | 
1、介绍Shiro的标签guest标签 :验证当前用户是否为“访客”,即未认证(包含未记住)的用户。
| 
1 
2 
3 
4 
5 | <shiro:guest> Hi
 there!  Please <a href="login.jsp">Login</a>
 or <a href="signup.jsp">Signup</a>
 today! </shiro:guest> | 
2、user标签 :认证通过或已记住的用户。
| 
1 
2 
3 
4 
5 | <shiro:user>     Welcome
 back John!  Not John? Click <a href="login.jsp">here<a>
 to login. </shiro:user> | 
3、authenticated标签 :已认证通过的用户。不包含已记住的用户,这是与user标签的区别所在。
| 
1 
2 
3 
4 
5 | <shiro:authenticated>     <a
 href="updateAccount.jsp">Update
 your contact information</a>. </shiro:authenticated> | 
4、notAuthenticated标签 :未认证通过用户,与authenticated标签相对应。与guest标签的区别是,该标签包含已记住用户。
| 
1 
2 
3 
4 
5 | <shiro:notAuthenticated>     Please
 <a href="login.jsp">login</a>
 in order to update your credit card information. </shiro:notAuthenticated> | 
5、principal 标签 :输出当前用户信息,通常为登录帐号信息。
| 
1 | Hello,
 <shiro:principal/>, how are you today? | 
6、hasRole标签 :验证当前用户是否属于该角色。
| 
1 
2 
3 
4 
5 | <shiro:hasRole
 name="administrator">     <a
 href="admin.jsp">Administer
 the system</a> </shiro:hasRole> | 
7、lacksRole标签 :与hasRole标签逻辑相反,当用户不属于该角色时验证通过。
| 
1 
2 
3 
4 
5 | <shiro:lacksRole
 name="administrator">     Sorry,
 you are not allowed to administer the system. </shiro:lacksRole> | 
8、hasAnyRole标签 :验证当前用户是否属于以下任意一个角色。
| 
1 
2 
3 
4 
5 | <shiro:hasAnyRoles
 name="developer,
 project manager, administrator">     You
 are either a developer, project manager, or administrator. </shiro:lacksRole> | 
9、hasPermission标签 :验证当前用户是否拥有指定权限。
| 
1 
2 
3 
4 
5 | <shiro:hasPermission
 name="user:create">     <a
 href="createUser.jsp">Create
 a newUser</a> </shiro:hasPermission> | 
10、lacksPermission标签 :与hasPermission标签逻辑相反,当前用户没有制定权限时,验证通过。
| 
1 
2 
3 
4 
5 | <shiro:hasPermission
 name="user:create">     <a
 href="createUser.jsp">Create
 a newUser</a> </shiro:hasPermission> | 
一、授权的三要素 
授权有着三个核心元素:权限、角色和用户。 
权限 
权限是Apache Shiro安全机制最核心的元素。它在应用程序中明确声明了被允许的行为和表现。一个格式良好好的权限声明可以清晰表达出用户对该资源拥有的权限。 
大多数的资源会支持典型的CRUD操作(create,read,update,delete),但是任何操作建立在特定的资源上才是有意义的。因此,权限声明的根本思想就是建立在资源以及操作上。 
而我们通过权限声明仅仅能了解这个权限可以在应用程序中做些什么,而不能确定谁拥有此权限。 
于是,我们就需要在应用程序中对用户和权限建立关联。 
通常的做法就是将权限分配给某个角色,然后将这个角色关联一个或多个用户。 
权限声明及粒度 
Shiro权限声明通常是使用以冒号分隔的表达式。就像前文所讲,一个权限表达式可以清晰的指定资源类型,允许的操作,可访问的数据。同时,Shiro权限表达式支持简单的通配符,可以更加灵活的进行权限设置。 
下面以实例来说明权限表达式。 
可查询用户数据 
User:view 
可查询或编辑用户数据 
User:view,edit 
可对用户数据进行所有操作 
User:* 或 user 
可编辑id为123的用户数据 
User:edit:123 
角色 
Shiro支持两种角色模式: 
1、传统角色:一个角色代表着一系列的操作,当需要对某一操作进行授权验证时,只需判断是否是该角色即可。这种角色权限相对简单、模糊,不利于扩展。 
2、权限角色:一个角色拥有一个权限的集合。授权验证时,需要判断当前角色是否拥有该权限。这种角色权限可以对该角色进行详细的权限描述,适合更复杂的权限设计。 
下面将详细描述对两种角色模式的授权实现。 
二、授权实现 
Shiro支持三种方式实现授权过程: 
| Subject方法 | 描述 | 
| hasRole(String roleName) | 当用户拥有指定角色时,返回true | 
| hasRoles(List<String> roleNames) | 按照列表顺序返回相应的一个boolean值数组 | 
| hasAllRoles(Collection<String> roleNames) | 如果用户拥有所有指定角色时,返回true | 
| Subject方法 | 描述 | 
| checkRole(String roleName) | 断言用户是否拥有指定角色 | 
| checkRoles(Collection<String> roleNames) | 断言用户是否拥有所有指定角色 | 
| checkRoles(String... roleNames) | 对上一方法的方法重载 | 
| Subject方法 | 描述 | 
| isPermitted(Permission p) | Subject拥有制定权限时,返回treu | 
| isPermitted(List<Permission> perms) | 返回对应权限的boolean数组 | 
| isPermittedAll(Collection<Permission> perms) | Subject拥有所有制定权限时,返回true | 
| Subject方法 | 说明 | 
| checkPermission(Permission p) | 断言用户是否拥有制定权限 | 
| checkPermission(String perm) | 断言用户是否拥有制定权限 | 
| checkPermissions(Collection<Permission> perms) | 断言用户是否拥有所有指定权限 | 
| checkPermissions(String... perms) | 断言用户是否拥有所有指定权限 | 
 
 原文:http://blog.csdn.net/huangyayong/article/details/51721579