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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值