您的位置:首页 > 编程语言 > Java开发

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

2017-02-13 17:03 961 查看
原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398

根据下载的pdf学习。

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

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

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

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

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


2.在控制器中使用注解

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

@RequiresRoles("admin")
@RequestMapping("/hello2")
public String hello2() {
return "success";
}


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

@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;
}


(3)权限注解

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: