shiro提供了相应的注解用于权限控制,如果使用这些注解就需要使用aop的功能来进行判断。shiro提供了spring aop集成,用于权限注解的解析和验证

shiro注解权限控制-5个权限注解

Shiro共有5个注解,接下来我们就详细说说吧

1.@RequiresAuthentication:

使用该注解标注的类,实例,方法在访问或调用时,当前Subject必须通过login 进行了身份验证;即 Subject.isAuthenticated() 返回 true

2.@RequiresUser:

表示当前 Subject 已经身份验证或者通过记住我登录的,才能访问或调用被该注解标注的类,实例,方法。

3.@RequiresGuest:

使用该注解标注的类,实例,方法在访问或调用时,当前Subject可以是“gust”身份,不需要经过身份验证或通过记住我登录过,即是游客身份

4.@RequiresRoles:

当前Subject必须拥有所有指定的角色时,才能访问被该注解标注的方法。如果当前Subject不同时拥有所有指定角色,则方法不会执行还会抛出AuthorizationException异常。

如:@RequiresRoles(value={“admin”, “user”}, logical= Logical.AND): 表示当前 Subject需要角色 admin 和user

5.@RequiresPermissions:

当前Subject需要拥有某些特定的权限时,才能执行被该注解标注的方法。如果当前Subject不具有这样的权限,则方法不会被执行。

如:@RequiresPermissions (value={“user:a”, “user:b”}, logical= Logical.OR):表示当前 Subject 需要权限 user:a 或user:b

使用方法:

Shiro的认证注解处理是有内定的处理顺序的,如果有个多个注解的话,前面的通过了会继续检查后面的,若不通过则直接返回,处理顺序依次为(与实际声明顺序无关)

RequiresRoles
RequiresPermissions
RequiresAuthentication
RequiresUser
RequiresGuest

例如:你同时声明了RequiresRoles和RequiresPermissions,那就要求拥有此角色的同时还得拥有相应的权限。

1.RequiresRoles的使用
可以用在Controller或者方法上。可以多个roles,多个roles时默认逻辑为 AND也就是所有具备所有role才能访问。

示例

//属于user角色
@RequiresRoles("user")

//必须同时属于user和admin角色
@RequiresRoles({"user","admin"})

//属于user或者admin之一;修改logical为OR 即可
@RequiresRoles(value={"user","admin"},logical=Logical.OR)

2.RequiresPermissions的使用
与 RequiresRoles类似

//符合index:hello权限要求
@RequiresPermissions("index:hello")

//必须同时复核index:hello和index:world权限要求
@RequiresPermissions({"index:hello","index:world"})

//符合index:hello或index:world权限要求即可
@RequiresPermissions(value={"index:hello","index:world"},logical=Logical.OR)

3.RequiresAuthentication,RequiresUser,RequiresGuest的使用
这三个的使用方法一样:

@RequiresAuthentication
@RequiresUser
@RequiresGusst

注:Shiro依赖于slf4j,commons-beanutils,commons-logging三个jar包。

RequiresRoles的使用:

1.在原有项目基础上 新建 service,编写如下方法

SrpingBoot Restful 接口权限_ci


2.修改controller,添加如下方法:

@Autowired
	private ShiroService shiroService;
	
	/**
	 * 测试 权限注解
	 * @return
	 */
	@RequestMapping("/testShiroAnnotation")
	public ModelAndView testShiroAnnotation(){
		shiroService.testMethod();
		return new ModelAndView("list");
	}

3.修改 applicationContext.xml,添加 service 扫码bean

<bean id="shiroService" class="com.example.shiro.service.ShiroService"></bean>

4.在 list.jsp页面添加 testShiroAnnotation 访问链接:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>list</title>
</head>
<body>
  
  <h3>list Page</h3>
  <div>欢迎:<shiro:principal></shiro:principal>登录</div>
  
  <shiro:hasRole name="admin">
     <a href="toAdmin">Admin page</a><br>
  </shiro:hasRole>
  
  <shiro:hasRole name="user">
    <a href="toUser">User page</a><br>
  </shiro:hasRole>
   <a href="testShiroAnnotation">测试 shiro 权限注解</a><br>
  <a href="logout">logout</a>
</body>
</html>

5.登录用户,进行测试

(1)登录admin:

SrpingBoot Restful 接口权限_身份验证_02


结果:控制台输出 方法信息

SrpingBoot Restful 接口权限_ci_03


(2)登录user,同样的方式测试 shiro权限注解,报错Subject does not have role [admin]

SrpingBoot Restful 接口权限_身份验证_04


测试成功