第十二章 与Spring集成(三) Shiro权限注解
2017-09-22 22:18
756 查看
跟我学Shiro第12章Demo(仅JAVA SE+Web+Shiro权限注解)
Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP 的功能来进行判断,如Spring AOP;Shiro 提供了Spring AOP 集成用于权限注解的解析和验证。为了测试,此处使用了Spring MVC来测试Shiro 注解,当然Shiro 注解不仅仅可以在web环境使用,在独立的JavaSE 中也是可以用的,此处只是以web 为例了。
在spring-mvc.xml配置文件添加Shiro Spring AOP权限注解的支持:
如上配置用于开启Shiro Spring AOP 权限注解的支持;<aop:config proxy-target-class="true">表示代理类。
接着就可以在相应的控制器(AnnotationController)中使用如下方式进行注解:
访问hello2方法的前提是当前用户有admin角色。
当验证失败,其会抛出UnauthorizedException异常,此时可以使用Spring的ExceptionHandler(DefaultExceptionHandler)来进行拦截处理:
如果集成Struts2,需要注意《Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效》问题:
http://jinnianshilongnian.iteye.com/blog/1850425
权限注解
表示当前Subject已经通过login 进行了身份验证;即Subject. isAuthenticated()返回true。
表示当前Subject已经身份验证或者通过记住我登录的。
表示当前Subject没有身份验证或通过记住我登录过,即是游客身份。
表示当前Subject需要角色admin 和user。
表示当前Subject需要权限user:a或user:b。
Shiro 提供了相应的注解用于权限控制,如果使用这些注解就需要使用AOP 的功能来进行判断,如Spring AOP;Shiro 提供了Spring AOP 集成用于权限注解的解析和验证。为了测试,此处使用了Spring MVC来测试Shiro 注解,当然Shiro 注解不仅仅可以在web环境使用,在独立的JavaSE 中也是可以用的,此处只是以web 为例了。
在spring-mvc.xml配置文件添加Shiro Spring AOP权限注解的支持:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="web"> <!-- 这两个注解其实可以忽略掉,意思是只扫描并实例化指定包下的Controller或ControllerAdvice注解的类 --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> </context:component-scan> <!-- Shiro Spring AOP权限注解的支持 --> <!-- 表示代理类 --> <aop:config proxy-target-class="true"></aop:config> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"></property> </bean> <!-- 平时访问页面都需要通过Controller,但有时候只想直接地跳转到目标页面,这时候就可以使用mvc:view-controller标签 path="/hello":就是你访问的路径(相当于RequestMapping("/hello")) view-name="hello":是你所要的视图(如hello.jsp,相当于return "hello") --> <mvc:view-controller path="/" view-name="index"/> <!-- 上面一行配置成功后,可能访问其他的页面会失败,不要慌,加下面这行--> <mvc:annotation-driven/> <!-- 默认的视图解析器,在上边的解析错误时使用(默认使用html) --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <property name="contentType" value="text/html"></property> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <!-- 控制 器异常处理 --> <bean id="exceptionHandlerExceptionResolver" class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean> <bean class="web.exception.DefaultExceptionHandler"></bean> </beans>
如上配置用于开启Shiro Spring AOP 权限注解的支持;<aop:config proxy-target-class="true">表示代理类。
接着就可以在相应的控制器(AnnotationController)中使用如下方式进行注解:
@Controller public class AnnotationController { @RequestMapping("/hello1") public String hello1(){ SecurityUtils.getSubject().checkRole("admin"); return "success"; } /* * 访问hello2方法的前提是当前用户有admin角色 */ @RequiresRoles("admin") @RequestMapping("/hello2") public String hello2(){ return "success"; } }
访问hello2方法的前提是当前用户有admin角色。
当验证失败,其会抛出UnauthorizedException异常,此时可以使用Spring的ExceptionHandler(DefaultExceptionHandler)来进行拦截处理:
@ControllerAdvice public class DefaultExceptionHandler { /** * 当出现 没有权限 异常 走这个方法 * 后续根据不同的需求定制即可 */ @ExceptionHandler({UnauthorizedException.class}) @ResponseStatus(HttpStatus.UNAUTHORIZED) public ModelAndView processUnauthenticatedException(NativeWebRequest request,UnauthorizedException e){ ModelAndView mv = new ModelAndView(); mv.addObject("exception",e); mv.setViewName("unauthorized"); return mv; } }
如果集成Struts2,需要注意《Shiro+Struts2+Spring3 加上@RequiresPermissions 后@Autowired失效》问题:
http://jinnianshilongnian.iteye.com/blog/1850425
权限注解
@RequiresAuthentication
表示当前Subject已经通过login 进行了身份验证;即Subject. isAuthenticated()返回true。
@RequiresUser
表示当前Subject已经身份验证或者通过记住我登录的。
@RequiresGuest
表示当前Subject没有身份验证或通过记住我登录过,即是游客身份。
@RequiresRoles(value={“admin”, “user”}, logical= Logical.AND)
表示当前Subject需要角色admin 和user。
@RequiresPermissions (value={“user:a”, “user:b”}, logical= Logical.OR)
表示当前Subject需要权限user:a或user:b。
相关文章推荐
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解
- Shiro与Spring集成时,Shiro权限注解@RequiresRoles等不生效的解决方案
- springmvc集成shiro注解权限
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制
- Shiro Springmvc 权限注解不生效
- 单点登录CAS与权限管理框架Shiro集成------Spring项目方式
- 【SSMS】springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制
- 2017.2.13 开涛shiro教程-第十二章-与Spring集成(一)配置文件详解
- Shiro权限控制框架 ---SpringMVC+Spring+My batis+Mysql+Maven集成开发Web项目
- SpringBoot 集成 shiro权限验证
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】
- 单点登录cas与权限管理框架shiro集成------spring项目方式
- 第八章:Shiro和Spring的集成——深入浅出学Shiro细粒度权限开发框架
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】
- Shiro集成spring--权限管理
- Shiro集成Spring之注解示例详解
- Shiro权限框架及与Spring集成
- 在前后端分离的SpringBoot项目中集成Shiro权限框架
- springmvc+spring+mybatis+maven项目集成shiro进行用户权限控制【转】