自定义配置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. dbunit进行DAO层Excel单元测试

    DAO层测试难点 可重复性,每次运行单元测试,得到的数据是重复的 独立性,测试数据与实际数据相互独立 数据库中脏数据预处理 不能给数据库中数据带来变化 DAO层测试方法 使用内存数据库,如H2.优点: ...

  2. js中new一个对象的过程

    使用new关键字调用函数(new ClassA(-))的具体步骤: 1. 创建空对象{}: 2. 设置新对象的constructor属性为构造函数的名称,设置新对象的__proto__属性指向构造函数 ...

  3. C++构造函数虚函数例题

    虚函数: #include <iostream> class A { public: A ():m_iVal() { test(); } virtual void func() { std ...

  4. gradle windows 环境变量

    我的电脑 ->  高级环境变量 GRADLE_HOME    D:\soft\gradle-3.5 path %GRADLE_HOME%\bin

  5. 放弃FreeMark?

    JAVA项目FreeMark 生产静态页面   a)    弊端                 i.          依赖后台工程师拼装页面,不利于前端.后台工作完全解耦. ii.         ...

  6. Javaweb---如何使用eclipse创建Javaweb项目

    在配置好--服务器Tomcat与Eclipse后,进行项目创建 配置地址:http://blog.csdn.net/baidu_37107022/article/details/71405194 流程 ...

  7. a标签实现文件下载

    如果想通过纯前端技术实现文件下载,直接把a标签的href属性设置为文件路径即可,如下: <a href="https://cdn.shopify.com/s/files/1/1545/ ...

  8. (转)C++——std::string类的引用计数

    1.概念 Scott Meyers在<More Effective C++>中举了个例子,不知你是否还记得?在你还在上学的时候,你的父母要你不要看电视,而去复习功课,于是你把自己关在房间里 ...

  9. DELPHI XE8 远程调试

    最近公司项目遇到问题需要远程调试搜索了一下怎么用 发现网上能找到最新的是XE2上的说明现在已经有一些不同了 按照上面的方法不能调试成功 经过测试XE8的方法如下:1.项目编译设置:2.在被调试电脑上运 ...

  10. JQuery与js具体使用的区别(不全,初学)

    jQuery能大大简化Javascript程序的编写 要使用jQuery,首先要在HTML代码最前面加上对jQuery库的引用,比如: <script language="javasc ...