2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398

根据下载的pdf学习。

第十二章-与Spring集成(二)shiro权限注解

shiro注解不仅可以使用在web环境,在独立的JavaSE中也是可以使用的。下面只以web为例。

shiro提供了spring aop集成用于权限注解的解析和验证。

1.在spring-mvc.xml文件中添加注解支持

1 <aop:config proxy-target-class="true"></aop:config>
2 <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
3     <property name="securityManager" ref="securityManager"/>
4 </bean>

2.在控制器中使用注解

(1)访问/hello2的前提是当前用户有admin角色

1 @RequiresRoles("admin")
2 @RequestMapping("/hello2")
3 public String hello2() {
4     return "success";
5 }

(2)验证失败抛出UnauthorizedException 异常

1 @ExceptionHandler({UnauthorizedException.class})
2 @ResponseStatus(HttpStatus.UNAUTHORIZED)
3 public ModelAndView processUnauthenticatedException(NativeWebRequest request,
4 UnauthorizedException e) {
5     ModelAndView mv = new ModelAndView();
6     mv.addObject("exception", e);
7     mv.setViewName("unauthorized");
8     return mv;
9 }

(3)权限注解

原文地址:https://www.cnblogs.com/lyh421/p/6394591.html