自定义配置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的更多相关文章

  1. Spring Security 自定义配置(1)

    @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapte ...

  2. 解决Spring Security自定义filter重复执行问题

    今天做项目的时候,发现每次拦截器日志都会打两遍,很纳闷,怀疑是Filter被执行了两遍.结果debug之后发现还真是!记录一下这个神奇的BUG! 问题描述 项目中使用的是Spring-security ...

  3. SPRING SECURITY JAVA配置:Web Security

    在前一篇,我已经介绍了Spring Security Java配置,也概括的介绍了一下这个项目方方面面.在这篇文章中,我们来看一看一个简单的基于web security配置的例子.之后我们再来作更多的 ...

  4. Spring Security 入门(1-2)Spring Security - 从 配置例子例子 开始我们的学习历程

    1.Spring Security 的配置文件 我们需要为 Spring Security 专门建立一个 Spring 的配置文件,该文件就专门用来作为 Spring Security 的配置. &l ...

  5. Spring Security 自定义登录页面

    SpringMVC + Spring Security,自定义登录页面登录验证 学习参考:http://www.mkyong.com/spring-security/spring-security-f ...

  6. spring security自定义指南

    序 本文主要研究一下几种自定义spring security的方式 主要方式 自定义UserDetailsService 自定义passwordEncoder 自定义filter 自定义Authent ...

  7. Spring Security认证配置(三)

    学习本章之前,可以先了解下上篇Spring Security认证配置(二) 本篇想要达到这样几个目的: 1.登录成功处理 2.登录失败处理 3.调用方自定义登录后处理类型 具体配置代码如下: spri ...

  8. Spring Security认证配置(二)

    学习本章之前,可以先了解下上篇Spring Security基本配置. 本篇想要达到这样几个目的: 1.访问调用者服务时,如果是html请求,则跳转到登录页,否则返回401状态码和错误信息 2.调用方 ...

  9. Spring Security认证配置(一)

    学习本章之前,可以先了解下上篇 Spring Security基本配置. 本篇主要讲述Spring Security基于表单,自定义用户认证配置(上篇中的配置,本篇将不再阐述).一共分为三步: 1.处 ...

随机推荐

  1. NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)

    NancyFx框架的自定义模块 新建一个空的Web项目 然后通过NuGet库安装下面的包 Nancy Nancy.Hosting.Aspnet 然后添加Models,Module,Views三个文件夹 ...

  2. APICloud框架——总结一下最近开发APP遇到的一些问题

    距离上一次发文都过去十天了, 下班回来懒的就想睡觉, 今天520一个重要的节日, 恩爱已经秀完, 该干点事情了!! 总结一下最近开发遇到的一些问题, 以及解决方案 纯css三角形 /* 没有哪个方向的 ...

  3. 无阻塞加载和defer、async

    无阻塞加载 把js放在head里,浏览器是怎么去执行它的呢,是按顺序加载还是并行加载呢?在旧的浏览器下,都是按照先后顺序来加载的,这就保证了加载的js依赖不会发生问题.但是少部分新的浏览器已经开始允许 ...

  4. JDBC与JNDI的区别

    程序员开发时,知道要开发访问MySQL数据库的应用,于是将一个对 MySQL JDBC 驱动程序类的引用进行了编码,并通过使用适当的 JDBC URL 连接到数据库. 就像以下代码这样: Connec ...

  5. cuda学习2-block与thread数量的选取

    由上一节可知,在main函数中,cuda程序的并行能力是在add<<<N,1>>>( dev_a, dev_b, dev_c )函数中体现的,这里面设置的是由N个b ...

  6. Simulation of empirical Bayesian methods (using baseball statistics)

    Previously in this series: The beta distribution Empirical Bayes estimation Credible intervals The B ...

  7. String为值类型还是引用类型

    关于String为值类型还是引用类型的讨论一直没有平息,最近一直在研究性能方面的问题,今天再次将此问题进行一次明确.希望能给大家带来点帮助. 如果有错误请指出. 来看下面例子: //值类型 int a ...

  8. netty基础--基本收发

    使用maven构建一个基本的netty收发应用,作为其他应用的基础.客户端使用packet sender工具. 1  添加netty依赖 1  maven netty依赖 <dependency ...

  9. Python爬虫学习(二) ——————爬取前程无忧招聘信息并写入excel

    作为一名Pythoner,相信大家对Python的就业前景或多或少会有一些关注.索性我们就写一个爬虫去获取一些我们需要的信息,今天我们要爬取的是前程无忧!说干就干!进入到前程无忧的官网,输入关键字&q ...

  10. Python进制转换(二进制、十进制和十六进制)

    #!/usr/bin/env python # -*- coding: utf-8 -*- # 2/10/16 base trans. wrote by srcdog on 20th, April, ...