自定义配置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. 如何优雅地运用 Chrome (Google)

    已经在很多工具类文章前言中,提及使用工具的重要性:以至于在写这篇时候,大为窘迫:穷尽了脑海中那些名句箴言,目测都已然在先前文章中被引用.鉴于杳让人心底意识到工具的重要性,并且能践行之,远比介绍如何使用 ...

  2. B计划

    简介:从一个初学者的角度来讲,要从六个方面来对计算机来做一个大约的了解: 计算机的组成: 电脑配置: 操作系统: CDEF盘: 正确的开关机(主要是关机): 常用软件: 1. 电脑由哪几部分组成?   ...

  3. 2017 UESTC Training for Graph Theory

    图论姿势太弱,这套题做了好久.. A:枚举最短那条边,然后最小生成树那种操作,1 和 n 联通就算答案 B:考虑到假如我们能凑出x的话,那很明显我们也能凑出任意数表示x + ai,考虑选取一个ai,然 ...

  4. [问题贴]mui.openWindow+自定义事件监听操作让alert()执行两次

    仔细看,Alert函数执行了两次 共两个页面:index.html和detail.html, detail.html为按钮设置了自定义事件监听(newsId),触发alert. 在index.html ...

  5. [原创]MongoDB综合实例一

    CentOS-6.5单机实现mongoDB分片 环境:1)CentOS 6.5系统      2)IP:本机3)MongoDB:MongoDB-linux-x86_64-2.6.1 实现:两个副本集s ...

  6. GitBook 使用

    介绍 GitBook是一个基于Node.js的命令行工具,可使用 Github/Git和Markdown来制作精美的电子书,GitBook 并非关 Git的教程. 导出格式有PDF.HTML等,需要添 ...

  7. Windows、Office系列产品精华部分集锦

    提示 有了这个帖子麻麻再也不用担心我因为四处找Microsoft家的软件和系统而四处劳累所烦恼了! 首先,你们最爱的老XP同志,XP同志虽然退休了,但是依然坚持在岗位上,向他致敬!! Windows ...

  8. Zepto源码分析-form模块

    源码注释 // Zepto.js // (c) 2010-2015 Thomas Fuchs // Zepto.js may be freely distributed under the MIT l ...

  9. WebService入门实例教程

    什么是WebService 通过使用WebService,您的应用程序可以向全世界发布信息,或提供某项功能,它是基于Web的服务,通过Web进行发布.查找和使用. WebService脚本平台需支持X ...

  10. ubuntu16.04 英文环境安装中文输入法

    1. 安装语言包 System Settings–>Language Support–>Install/Remove Languages 选中chinese,点击Apply应用即可,等待下 ...