自定义配置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. AJAX的简单梳理

    一.Ajax的简介 Ajax,异步JavaScript和XML,异步提交,无刷新技术,局部刷新技术 1)传统的客户端与服务器端交互的模式 客户端发送请求直接给服务端,服务接受到请求后进行逻辑运算,最终 ...

  2. three.js粒子效果(分别基于CPU&GPU实现)

    前段时间做了一个基于CPU和GPU对比的粒子效果丢在学习WebGL的群里,技术上没有多作讲解,有同学反馈看不太懂GPU版本,干脆开一篇文章,重点讲解基于GPU开发的版本. 一.概况 废话不多说,先丢上 ...

  3. JAVA 编程规范

       软件开发技术规范 PTHINK-DEVELOP-JAVA-091010         Java语言编程规范   2009-10-10发布                2009-10-11实施 ...

  4. 学java网络编程的心得体会

    网络编程简单思路 一.发送端1创建udp服务,通过DatagramSocket对象;2确定数据,封装成包DatagramPacket(byte[] buf, int length, InetAddre ...

  5. php系统共享模板问题

    我们在用php+html+css来写一个管理系统时,例如报名系统.投票系统,统计系统等,我们往往需要在管理后台查看每一位报名者的情况,问题接着就来了,由于比赛或者活动要求不同个,往往报名表不太一样,这 ...

  6. 初入计算机图形学——BVH结构的实现

    摘要: 本人水平有限,若有错误也请指正~ 光线追踪作为全局光照解决方案的一个重要思想,其与场景大量三角形的求交效率高低直接影响最终算法的速度,典型的一些渲染器都采用二叉树来将场景进行划分,最近自己实现 ...

  7. VMware Workstation 11安装

    VMware Workstation 11序列号:1F04Z-6D111-7Z029-AV0Q4-3AEH8

  8. Common.Logging源码解析一

    Common.Logging是Apache下的一个开源日志接口组件,主要用于切换不同的日志库,因为当前流行的日志库有很多向log4j.log4net(log4j的.net版本)等等,所以为了能灵活的切 ...

  9. 禁止右键,禁止选中,禁止网页复制的Js代码

    document.oncontextmenu=new Function('event.returnValue=false;'); document.onselectstart=new Function ...

  10. Coursera 机器学习笔记(八)

    主要为第十周内容:大规模机器学习.案例.总结 (一)随机梯度下降法 如果有一个大规模的训练集,普通的批量梯度下降法需要计算整个训练集的误差的平方和,如果学习方法需要迭代20次,这已经是非常大的计算代价 ...