spring-security配置和原理简介
SpringSecurity3的核心类有三种
2.资源权限获取器:用来取得访问某个URL或者方法所需要的权限,接口为SecurityMetadataSource
3.访问决策器:用来决定用户是否拥有访问权限的关键类,其接口为AccessDecisionManager。
调用顺序为:AbstractSecurityInterceptor调用SecurityMetadataSource取得资源的所有可访问权限,然后再调用AccessDecisionManager来实现决策,确定用户是否有权限访问该资源。
SecurityMetadataSource包括MethodSecurityMetadataSource和FilterInvocationSecurityMetadataSource,分别对应方法和URL资源。
你也可以完全自定义自己的过滤器、资源权限获取器、访问决策器,下面给出完整的springsecurity3的配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans:beans xmlns="http://www.springframework.org/schema/security"
- xmlns:beans="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/security
- http://www.springframework.org/schema/security/spring-security-3.0.xsd">
- <beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener" />
- <http auto-config="true" access-denied-page="/403.jsp">
- <intercept-url pattern="/css/**" filters="none" />
- <intercept-url pattern="/images/**" filters="none" />
- <intercept-url pattern="/js/**" filters="none" />
- <intercept-url pattern="/403.jsp" filters="none" />
- <intercept-url pattern="/" filters="none" />
- <intercept-url pattern="/login.jsp" filters="none" />
- <form-login login-page="/login.jsp" authentication-failure-url="/login.jsp?error=true" default-target-url="/finance/index.do?listId=CONSUMPTION&page.rowCount=10" />
- <logout logout-success-url="/login.jsp"/>
- <!-- 防止同一用户多次登录,使第二次登录失败 -->
- <session-management>
- <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
- </session-management>
- <!-- 增加一个filter,这点与Acegi是不一样的,不能修改默认的filter了,这个filter位于FILTER_SECURITY_INTERCEPTOR之前 -->
- <custom-filter before="FILTER_SECURITY_INTERCEPTOR" ref="urlSecurityFilter" />
- </http>
- <!-- 一个自定义的filter,必须包含authenticationManager,accessDecisionManager,securityMetadataSource三个属性,
- 我们的所有控制将在这三个类中实现,解释详见具体配置 -->
- <beans:bean id="urlSecurityFilter" class="com.maxjay.main.system.web.filter.UrlSecurityInterceptorFilter">
- <beans:property name="authenticationManager" ref="authenticationManager" />
- <beans:property name="accessDecisionManager" ref="securityAccessDecisionManager" />
- <beans:property name="securityMetadataSource" ref="urlSecurityMetadataSource" />
- </beans:bean>
- <!-- 认证管理器,实现用户认证的入口,主要实现UserDetailsService接口即可 -->
- <authentication-manager alias="authenticationManager">
- <authentication-provider user-service-ref="userService">
- <!-- 如果用户的密码采用加密的话,可以加点“盐”
- <password-encoder hash="md5" />
- -->
- </authentication-provider>
- </authentication-manager>
- </beans:beans>
其中userService实现了UserDetailsService用来与自己的用户表进行适配,UrlSecurityInterceptorFilter继承了AbstractSecurityInterceptor并实现了Filter接口,urlSecurityMetadataSource实现了FilterInvocationSecurityMetadataSource接口,securityAccessDecisionManager实现了AccessDecisionManager接口,它们都通过spring的注解声明为容器的一个对象,所以在配置文件中才能直接引用。
springsecurity3有提供了几个已经实现好的访问决策器,其抽象类为AbstractAccessDecisionManager,它使用投票机制(AccessDecisionVoter)来确定用户有没有权限访问某资源,其实现类有三个:
AffirmativeBased:只要有一个投票器通过即审核通过
ConsensusBased:只有当赞成票>反对票时 审核才会通过
UnanimousBased:只要有一个投票器反对,审核就不通过
你可以直接使用该抽象类的实现类,其配置如下:
- <beans:bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
- <!-- false意味着当配置的投票器只投了弃权票时,不允许继续执行 -->
- <beans:property name="allowIfAllAbstainDecisions" value="false"/>
- <!-- 配置该决策器所需要的投票器 -->
- <beans:property name="decisionVoters">
- <beans:list>
- <beans:bean class="org.springframework.security.access.vote.RoleVoter"/>
- <beans:bean class="org.springframework.security.access.vote.AuthenticatedVoter"/>
- </beans:list>
- </beans:property>
- </beans:bean>
还是不懂的话,看看这篇文章应该容易理解一点
http://www.blogjava.net/youxia/archive/2008/12/07/244883.html
spring-security配置和原理简介的更多相关文章
- spring boot 之 spring security 配置
Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...
- 深入理解Spring Security授权机制原理
原创/朱季谦 在Spring Security权限框架里,若要对后端http接口实现权限授权控制,有两种实现方式. 一.一种是基于注解方法级的鉴权,其中,注解方式又有@Secured和@PreAuth ...
- spring security 配置多个AuthenticationProvider
前言 发现很少关于spring security的文章,基本都是入门级的,配个UserServiceDetails或者配个路由控制就完事了,而且很多还是xml配置,国内通病...so,本文里的配置都是 ...
- Spring学习日志之Spring Security配置
依赖引入 <dependency> <groupId>org.springframework.security</groupId> <artifactId&g ...
- Spring Security入门(2-3)Spring Security 的运行原理 4 - 自定义登录方法和页面
参考链接,多谢作者: http://blog.csdn.net/lee353086/article/details/52586916 http元素下的form-login元素是用来定义表单登录信息的. ...
- Spring Security配置
更加优雅地配置Spring Securiy(使用Java配置和注解):https://www.cnblogs.com/xxzhuang/p/5960001.html 采用注解方式实现security: ...
- Spring Security配置个过滤器也这么卷
以前胖哥带大家用Spring Security过滤器实现了验证码认证,今天我们来改良一下验证码认证的配置方式,更符合Spring Security的设计风格,也更加内卷. CaptchaAuthent ...
- spring mvc 和spring security配置 web.xml设置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmln ...
- 通过 Spring Security配置 解决X-Frame-Options deny 造成的页面空白 iframe调用问题
spring Security下,X-Frame-Options默认为DENY,非Spring Security环境下,X-Frame-Options的默认大多也是DENY,这种情况下,浏览器拒绝当前 ...
- spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置
spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
随机推荐
- 123457123456#0#-----com.threeapp.HeiXIanBuNengPeng01----黑线不能碰
-com.threeapp.HeiXIanBuNengPeng01----黑线不能碰
- PHP爬虫最全总结2-phpQuery,PHPcrawer,snoopy框架中文介绍
第一篇文章介绍了使用原生的PHP和PHP的扩展库实现了爬虫技术.本文尝试使用PHP爬虫框架来写,首先对三种爬虫技术phpQuery,PHPcrawer, snoopy进行对比,然后分析模拟浏览器行为的 ...
- XenServer 根分区空间满的解决办法
1.清除已经应用的旧补丁文件 删除 /var/patch/ 下的除 applied 之外的所有文件 2.清除旧版的Xen-Tools文件 删除 /opt/xensource/packages/iso/ ...
- LVS负载均衡在Ubuntu环境下部署详解
一.本地环境介绍: 负载均衡的三台机器均为Ubuntu Server 14.04 64位系统,内核中已集成ipvs模块( modprobe -l | grep ipvs 查看 ).为演示LVS负载均衡 ...
- Asp.net SignalR 实现服务端消息实时推送到所有Web端
ASP .NET SignalR是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信.实际上 Asp.net SignalR 2 实现 服务端消息推送到Web端, 更加 ...
- Evaluating Automatically Generated timelines from the Web (paper1)
摘要: 问题:There is a need that 以一个更全面/更综合的方式来展现搜索结果.对此,作者正在开发一个系统,called “Cronopath”,这个系统将产生一个时间线,通过决定每 ...
- [转帖]linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习)
linux常用命令大全(linux基础命令入门到精通+实例讲解+持续更新+命令备忘录+面试复习) https://www.cnblogs.com/caozy/p/9261224.html 总结的挺好的 ...
- Jmeter做HTTP接口测试
接口测试概述:https://www.cnblogs.com/mawenqiangios/p/7886115.html Jmeter接口测试教程:https://www.cnblogs.com/hou ...
- PAT(B)1003 我要通过!(Java)
1003 我要通过! 题目 判断字符串是否符合给定的规则.更多内容点击标题. 参考博客 ValarMorghulis的博客 分析 规律:num_a * num_b = num_c.字符串a中字 ...
- electron实现透明点投的方法
1. electron createWindow 的时候 设置 transparent: true, clickThrough: 'pointer-events' 2. body 上添加 pointe ...