基于java config的springSecurity(四)--启用全局方法安全
来源:互联网 发布:海康视频矩阵128进24出 编辑:程序博客网 时间:2024/05/21 10:27
1.使用@EnableGlobalMethodSecurity注解,可以直接标注之前的SecurityConfig上面.也可以独立出来一个配置MethodSecurityConfig,继承GlobalMethodSecurityConfiguration可以实现更加复杂的操作(比如重写createExpressionHandler方法实现自定义的MethodSecurityExpressionHander).
然后把这个MethodSecurityConfig加入到RootConfigClasses.更改org.exam.config.DispatcherServletInitializer#getRootConfigClasses
把这段代码加到SecurityConfig.
2.在方法(类或接口上)添加注解来限制方法的访问.我们使用prePostEnabled(从参考文档介绍,要比securedEnabled和jsr250Enabled强大).例如:
3.直接修改相应数据库记录,让一个用户有user_query权限,一个用户没有user_query权限来测试.在单元测试添加了初始化的数据.
还有,参考文档有安全表达试的介绍,比如下面代表的意思:传入的联系人为当前的认证用户,才可以执行doSomething方法
@PreAuthorize("#c.name == authentication.name")
public void doSomething(@P("c")Contact contact);
从我的测试结果来看,一旦用户被拒绝访问,就会返回一个403,使用jetty9.2.2.v20140723发现返回的response的Content-Type是text/html; charset=ISO-8859-1.所以出现了乱码.但使用tomcat 8.0.14下部署是不会乱码的.
跟踪源码到org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper#sendError(int, java.lang.String),发现这个Response还是UTF-8,再跟下去就到javax.servlet.http.HttpServletResponseWrapper#sendError(int, java.lang.String),从此处基本可判断乱码的引起与spring security无关.继续到org.eclipse.jetty.server.Response#sendError(int, java.lang.String),有一句setContentType(MimeTypes.Type.TEXT_HTML_8859_1.toString());当然,这个403要处理,可在SecurityConfig#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)配置错误页,比如
and().exceptionHandling().accessDeniedPage("/exception/403");注意mvc要能正常解析这个url,并且权限也要忽略这个url.然后在页面通过${SPRING_SECURITY_403_EXCEPTION}可以获取这个异常(为什么是SPRING_SECURITY_403_EXCEPTION,可参考org.springframework.security.web.access.AccessDeniedHandlerImpl#handle)
如果把权限放在web层(比如@PreAuthorize放在spring mvc的Controller方法上),那就将MethodSecurityConfig加入到ServletConfigClasses.如果希望在web和service层都要做权限控制,就把所有的Config放到RootConfigClasses,ServletConfigClasses留空.解析一下为什么要这么做:
实现这个启用全局方法安全是通过spring aop实现的.一旦使用了@EnableGlobalMethodSecurity注解,就会注册一个名为org.springframework.aop.framework.autoproxy.InfrastructureAdvisorAutoProxyCreator的BeanPostProcessor.它的作用就是将那些使用了@PreAuthorize之类的类生成代理.但前提下,通过这个方式生成代理的目标Bean,它至少要和这个BeanPostProcessor在同一个BeanFactory(上下文).进一步来看看org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization:
这个beanPostProcessors是在实例化spring容器时,将对应的beanPostProcessor添加到对应的spring容器.假设用户自定义的Bean和这个beanPostProcessor不在同一个上下文,那么自然它不会受这个BeanPostProcessor的影响.剩下的就不难解析了.还有要注意使用@EnableWebMvcSecurity或@EnableWebSecurity来定义springSecurityFilterChain Bean的Configuration类应只能放在RootApplicationContext下面,至于原因可看http://blog.csdn.net/xiejx618/article/details/50603758文末
源码:http://download.csdn.net/detail/xiejx618/8366505
启用全局方法安全是通过spring aop实现的,具体看:<spring aop(十)--spring security启用全局方法使用aop的分析>
- 顶
- 0
- 踩
- 基于java config的springSecurity(四)--启用全局方法安全
- 基于java config的springSecurity(四)--启用全局方法安全
- 基于java config的springSecurity(三)--加入RememberMe,启用CSRF和增强密码
- 基于java config的springSecurity(三)--加入RememberMe,启用CSRF和增强密码
- 基于java config的springSecurity--单元测试
- 基于java config的springSecurity--单元测试
- 基于java config的springSecurity(一)--基本搭建
- 基于java config的springSecurity(二)--自定义认证
- 基于java config的springSecurity(五)--session并发控制
- 基于java config的springSecurity(六)--集成spring session
- 基于java config的springSecurity(一)--基本搭建
- 基于java config的springSecurity(二)--自定义认证
- 基于java config的springSecurity(五)--session并发控制
- 基于java config的springSecurity(六)--集成spring session
- 基于java config的springSecurity(二)--自定义认证
- SpringSecurity基于方法保护
- 基于rest的SpringSecurity(完整版,相对最安全版本)
- 基于rest的SpringSecurity(基于认证)
- 110. Balanced Binary Tree
- T-SQL查询进阶--理解SQL SERVER中的分区表
- 运算符操作
- ffmpeg源码简析 结构总览
- html5使用<marquee>标签实现走马灯效果
- 基于java config的springSecurity(四)--启用全局方法安全
- JAVA编程中性能优化的一些地方
- 浅谈黑盒测试和白盒测试
- Java并发编程之Lock
- 配置文件格式的选择
- 浅谈链式编程及建造者模式
- YUV420格式学习
- SpringBoot常用配置介绍
- 仅为记录 电商项目配置文件 web Spring mabitis