Spring Security-自定义配置Filter
自定义配置Filter
一.最基础的配置
SecurityContextPersistenceFilter
用来建立 SecurityContext,而它被用来贯穿整个 request 过程以跟踪请求者的认证信息。
<bean id="securityContextPersistenceFilter"
class="org.springframework.security.web.context.SecurityContextPersistenceFilter/>
UsernamePasswordAuthenticationFilter
用来处理 form 交并检查认证存储是否为合法凭证
<bean id="UsernamePasswordAuthenticationFilter"
class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
<property name="authenticationManager" ref="customAuthenticationManager"/>
<!-- 高级配置 添加rememberMe-->
<property name="rememberMeServices" ref="rememberMeServices"/>
</bean>
AnonymousAuthenticationFilter
站点允许匿名访问。尽管对于比较特殊的条件 AnonymousAuthenticationFilter 并 不需要,但是通常情况下会使用它,因为只对请求添加了一点的预处理。
<bean id="anonymousAuthenticationFilter"
class="org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
<property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS"/>
<property name="key" value="BF93JFJ091N00Q7HF"/>
</bean>
FilterSecurityInterceptor
最终负责检查 Authentication,这个过滤器确定一个特定的请求最终是被拒绝还是被接受。
<bean id="filterSecurityInterceptor"
class="org.springframework.security.web.access.intercept.FilterSecurityInterceptor">
<property name="authenticationManager" ref="customAuthenticationManager"/>
<property name="accessDecisionManager" ref="affirmativeBased"/>
<property name="securityMetadataSource">
<security:filter-security-metadata-source>
<security:intercept-url pattern="/login.do" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/home.do" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/account/*.do" access="ROLE_USER"/>
<security:intercept-url pattern="/*" access="ROLE_USER"/>
</security:filter-security-metadata-source>
</property>
</bean>
配置最少的支持对象集合
<!--投票器配置-->
<bean class="org.springframework.security.access.vote.AffirmativeBased" id="affirmativeBased">
<property name="decisionVoters">
<list>
<ref bean="roleVoter"/>
<ref bean="authenticatedVoter"/>
</list>
</property>
</bean>
<bean class="org.springframework.security.access.vote.RoleVoter" id="roleVoter"/>
<bean class="org.springframework.security.access.vote.AuthenticatedVoter"id="authenticatedVoter"/>
<!--customAuthenticationManager 配置 提供 Authentication信息-->
<bean id="customAuthenticationManager"
class="org.springframework .security.authentication.ProviderManager">
<property name="providers">
<list>
<ref local="daoAuthenticationProvider"/>
<ref local=”anonymousAuthenticationProvider”/>
<!-- 此处为高级配置 添加rememberme-->
<ref local="rememberMeAuthenticationProvider"/>
</list>
</property>
</bean>
<bean id="daoAuthenticationProvider"
class="org.springframework .security.authentication.dao.DaoAuthenticationProvider">
<property name="userDetailsService" ref="jdbcUserService"/>
</bean>
<bean id=”anonymousAuthenticationProvider”
class=”org.springframework.security.authentication.AnonymousAuthenticationProvider”>
<property name=”key” value=”BF93JFJ091N00Q7HF”/>
</bean>
二 高级配置
Session 生命周期的调整元素
AbstractAuthenticationProcessingFilter (UsernamePasswordAuthenticationFilter 的父类)
属性:allowSessionCreation 默认值:true
如果为 true,当认证失败时创建一个 新的 session(存储异常)
UsernamePasswordAuthenticationFilter
属性:tearllowSessionCreation 默认值:true
如果为 true 的话, 这个特殊的过滤器将会创建一个 session 存储最后尝试的用户名。
SecurityContextLogoutHandler
属性:invalidateHttpSession 默认值:true
如果为 true, HttpSession 将会失效(参考 Servlet 规 范了解 session 失效 的细节)
SecurityContextPersistenceFilter
属性:forceEagerSessionCreation 默认值:false
如果为 true,该过 滤器将会在执行链中其它过滤器之前 创建一个 session。
HttpSessionSecurityContextRepository
属性:allowSessionCreation 默认值:true
如果为 true,如果 在请求结束时 session 中还没有 SecurityContext 的 话,SecurityContext 将存储到 session 中。
手动配置其它通用的服务
声明缺失的过滤器 添加三个我们还没有配置的服务。包含处理退出、 remember me 以及异常转换
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<security:filter-chain-map path-type="ant">
<security:filter-chain pattern="/**" filters=" securityContextPersistenceFilter, logoutFilter,
usernamePasswordAuthenticationFilter, rememberMeAuthenticationFilter,
anonymousAuthenticationFilter, exceptionTranslationFilter, filterSecurityInterceptor" />
</security:filter-chain-map>
</bean>
<!--LogoutFilter-->
<bean id="logoutFilter" class="org.springframework.security .web.authentication.logout.LogoutFilter">
<!-- the post-logout destination -->
<constructor-arg value="/"/>
<constructor-arg>
<array>
<ref local="logoutHandler"/>
<!--高级配置 添加 rememberMe-->
<ref local="rememberMeServices"/>
</array>
</constructor-arg>
<property name="filterProcessesUrl" value="/logout"/>
</bean>
<bean id="logoutHandler"
class="org.springframework.security .web.authentication.logout.SecurityContextLogoutHandler"/>
<!--RememberMeAuthenticationFilter-->
<bean id="rememberMeAuthenticationFilter"
class="org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter">
<property name="rememberMeServices" ref="rememberMeServices"/>
<property name="authenticationManager" ref="customAuthenticationManager" />
</bean>
<bean id="rememberMeServices"
class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
<property name="key" value="jbcpPetStore"/>
<property name="tokenValiditySeconds" value="3600"/>
<property name="tokenRepository" ref="jdbcRememberMeTokenRepository"/>
<property name="userDetailsService" ref="jdbcUserService"/>
</bean>
<bean id="jdbcRememberMeTokenRepository"
class="org.springframework.security.web .authentication.rememberme.JdbcTokenRepositoryImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="rememberMeAuthenticationProvider"
class="org.springframework.security .authentication.RememberMeAuthenticationProvider">
<property name="key" value="jbcpPetStore"/>
</bean>
<!--ExceptionTranslationFilter-->
<bean id="exceptionTranslationFilter"
class="org.springframework.security.web .access.ExceptionTranslationFilter">
<property name="authenticationEntryPoint" ref="authenticationEntryPoint"/>
<property name="accessDeniedHandler" ref="accessDeniedHandler"/>
</bean>
<bean id="authenticationEntryPoint"
class="org.springframework.security.web .authentication.LoginUrlAuthenticationEntryPoint">
<property name="loginFormUrl" value="/login.do"/>
</bean>
<bean id="accessDeniedHandler"
class="org.springframework.security.web .access.AccessDeniedHandlerImpl">
<property name="errorPage" value="/accessDenied.do"/>
</bean>
明确配置 SpEL 表达式和投票器
<!--重新配置了 affirmativeBased 该类会被AccessDecisionManager使用-->
<bean class="org.springframework.security.access.vote.AffirmativeBased" id="affirmativeBased">
<property name="decisionVoters">
<list>
<ref bean="expressionVoter"/>
</list>
</property>
</bean>
<bean class="org.springframework.security.web.access .expression.WebExpressionVoter" id="expressionVoter">
<property name="expressionHandler" ref="expressionHandler"/>
</bean>
<bean class="org.springframework.security.web.access.expression.DefaultWebSecurityExpressionHandler"
id="expressionHandler"/>
Spring Security-自定义配置Filter的更多相关文章
- Spring Security 自定义配置(1)
@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapte ...
- 解决Spring Security自定义filter重复执行问题
今天做项目的时候,发现每次拦截器日志都会打两遍,很纳闷,怀疑是Filter被执行了两遍.结果debug之后发现还真是!记录一下这个神奇的BUG! 问题描述 项目中使用的是Spring-security ...
- SPRING SECURITY JAVA配置:Web Security
在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...
- Spring Security 入门(1-2)Spring Security - 从 配置例子例子 开始我们的学习历程
1.Spring Security 的配置文件 我们需要为 Spring Security 专门建立一个 Spring 的配置文件,该文件就专门用来作为 Spring Security 的配置. &l ...
- Spring Security 自定义登录页面
SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...
- spring security自定义指南
序 本文主要研究一下几种自定义spring security的方式 主要方式 自定义UserDetailsService 自定义passwordEncoder 自定义filter 自定义Authent ...
- Spring Security认证配置(三)
学习本章之前,可以先了解下上篇Spring Security认证配置(二) 本篇想要达到这样几个目的: 1.登录成功处理 2.登录失败处理 3.调用方自定义登录后处理类型 具体配置代码如下: spri ...
- Spring Security认证配置(二)
学习本章之前,可以先了解下上篇Spring Security基本配置. 本篇想要达到这样几个目的: 1.访问调用者服务时,如果是html请求,则跳转到登录页,否则返回401状态码和错误信息 2.调用方 ...
- Spring Security认证配置(一)
学习本章之前,可以先了解下上篇 Spring Security基本配置. 本篇主要讲述Spring Security基于表单,自定义用户认证配置(上篇中的配置,本篇将不再阐述).一共分为三步: 1.处 ...
随机推荐
- ar1220f-s四条拨号光纤做的策略路由实现负载均衡
acl number 3001 //内网数据流不需被重定向到外网出口. rule 5 permit ip source 192.168.0.0 0.0.255.255 destination 19 ...
- Java类加载和卸载的跟踪
博客搬家自https://my.oschina.net/itsyizu/blog/ 什么是类的加载和卸载 Java程序的运行离不开类的加载,为了更好地理解程序的执行,有时候需要知道系统加载了哪些类.一 ...
- 使用SpringBoot快速构建应用程序
1.Spring MVC和Spring Boot自带的web构建方式有所区别.Spring提供了spring-boot-starter-web自动配置模块. 2. 添加如下依赖 <depende ...
- UIWebView 跳过HTTPS证书认证
UIWebView跳过证书认证 在UIWebView中加入如下代码即可(Error Domain=NSURLErrorDomain Code=-1202) //跳过证书验证 @interface NS ...
- SparkMLlib回归算法之决策树
SparkMLlib回归算法之决策树 (一),决策树概念 1,决策树算法(ID3,C4.5 ,CART)之间的比较: 1,ID3算法在选择根节点和各内部节点中的分支属性时,采用信息增益作为评价标准.信 ...
- AtomicInteger的使用
JDK API 1.7相关介绍 可以用原子方式更新的 int 值.有关原子变量属性的描述,请参阅 java.util.concurrent.atomic 包规范.AtomicInteger 可用在应用 ...
- [python] 1、python鼠标点击、移动事件应用——写一个自动下载百度音乐的程序
1.问题描述: 最近百度总爱做一些破坏用户信任度的事——文库金币变券.网盘限速,吓得我赶紧想办法把存在百度云音乐中的歌曲下载到本地. http://yinyueyun.baidu.com/ 可问题是云 ...
- JS 使用 splice() 对数组去重
一 问题 有如下 js 数组 connect_clients,需要在去掉所有元素中 user_id, goods_id 这两者的值都相同的元素. [ { id: 'eff040fb-92bc-4f24 ...
- canvas——路径搜索
在前一篇博客中随机生成迷宫,现在就以随机生成的迷宫为地图,开始寻找路径. 迷宫寻路也可以使用DFS,BFS,但常见的是A*算法,它是启发式搜索算法的一种,效率相比前两者也更高.接下来以A*算法为例,迷 ...
- Elasticsearch 与 Kafka 整合剖析
1.概述 目前,随着大数据的浪潮,Kafka 被越来越多的企业所认可,如今的Kafka已发展到0.10.x,其优秀的特性也带给我们解决实际业务的方案.对于数据分流来说,既可以分流到离线存储平台(HDF ...