Spring Security调研记录【三】--实现Method Security
来源:互联网 发布:一个月练宽肩膀知乎 编辑:程序博客网 时间:2024/05/12 19:27
Method Security 通过JSR-250或Spring注解、或AspectJ的pointcuts,对Service层的Method进行Security控制。
Method Security的配置比较简单,只需在Context中定义标签<global-method-security> 即可 ,<global-method-security>对本Cotext的bean有效。
一、Method Security提供四种配置:
1、@Secured注解
<global-method-security secured-annotations="enabled" />
public interface BankService {@Secured("IS_AUTHENTICATED_ANONYMOUSLY")public Account readAccount(Long id);@Secured("IS_AUTHENTICATED_ANONYMOUSLY")public Account[] findAccounts();@Secured("ROLE_TELLER")public Account post(Account account, double amount);}
2、JSR-250 annotations支持
<global-method-security jsr250-annotations="enabled" />
3、expression-based syntax
<global-method-security pre-post-annotations="enabled" />
public interface BankService {@PreAuthorize("isAnonymous()")public Account readAccount(Long id);@PreAuthorize("isAnonymous()")public Account[] findAccounts();@PreAuthorize("hasAuthority('ROLE_TELLER')")public Account post(Account account, double amount);}
4、protect-pointcut
protect-pointcut可同时对多个Bean方法进行权限配置
<global-method-security><protect-pointcut expression="execution(* com.mycompany.*Service.*(..))" access="ROLE_USER"/></global-method-security>
二、Method Security 的认证(Authentication)与授权(Authorization)失败处理
Method Security并不是在Filter中进行Access Decision,而是在DispatcherServlet的AOP中进行。所以异常可通过Spring MVC的统一异常提醒捕捉到。
在Method Security中,无论是未认证或无权访问的异常都只是会抛出AccessDeniedException异常。
因为,对于未认证的请求,在FilterChainProxy的Filter运行后中会生成一个匿名(Anonymous)Authentication对象,在做是否有权访问判定时,就用Anonymous的Authentication进行权限判定,所以就只有AccessDeniedException。
所以要区分是Authentication异常还是Authorization异常,需要借用AuthenticationTrustResolver类的isAnonymous(Authentication)方法,判定当前是否为Anonymous。
- Spring Security调研记录【三】--实现Method Security
- Spring Security调研记录【五】--基于Cas实现单点登录
- Spring Security调研记录【七】--核心模型与实现
- Spring Security调研记录【一】--实现基本认证与Url权限控制
- Spring Security调研记录【二】--实现异步Json请求的基本认证与Url权限控制
- spring security 学习记录
- spring security(三)
- Spring Security教程(三)
- Spring Security学习记录(三) -- JSON Web Token实践(上)
- spring security 3 自定义(三)
- Spring security学习笔记三
- spring security 初次涉猎三
- Spring Security学习记录(一) -- 初识Spring Security
- Spring Security学习记录(二) -- Spring Security的Filter
- 【Spring Security实战系列】Spring Security实战(三)
- spring security实现传参
- Spring Security 实现身份认证
- Spring Security实现权限管理
- 大数据Spark:动手写WordCount
- 项目管理及自动构建工具Maven
- json解析
- 将pdf转换为ppt的一些基本方法
- SpringMVC传递json格式数据
- Spring Security调研记录【三】--实现Method Security
- 天声人語 20150526
- scala练习2
- C++运算符重载(7) - 赋值操作符能否被继承
- java 分页工具类
- 日经春秋 20150526
- pdf转换成txt的制作方法
- 移动IM开发那些事:技术选型和常见问题
- #169 Majority Element