【问题标题】:Configure Spring Security to use custom UsernamePasswordAuthenticationFilter配置 Spring Security 以使用自定义 UsernamePasswordAuthenticationFilter
【发布时间】:2011-12-05 08:57:54
【问题描述】:

我已经实现了自己的LowerCaseUsernamePasswordAuthenticationFilter,它只是UsernamePasswordAuthenticationFilter 的子类。

但现在我的问题是,如何配置 Spring 安全性以使用此过滤器。

到现在我用过:

<security:http auto-config="true" use-expressions="true">
    <security:form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
    <security:logout logout-url="/resources/j_spring_security_logout" />

    <security:intercept-url pattern="/**" access="isAuthenticated()" requires-channel="${cfma.security.channel}" />
</security:http>

我真的要关闭auto-config 并需要手动配置所有过滤器吗? - 如果这是真的,有人可以提供一个例子吗?


简单的添加security:custom-filter的方法:

<security:http ...>

   <security:form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
   <security:custom-filter ref="lowerCaseUsernamePasswordAuthenticationFilter" position="FORM_LOGIN_FILTER"/>
   ...
 </security:http>

确实会导致该消息出现异常:

配置问题:过滤bean&lt;lowerCaseUsernamePasswordAuthenticationFilter&gt;和'Root bean: class [org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter];范围=;摘要=假;懒惰初始化=假;自动线模式=0;依赖检查=0;自动接线候选=真;主要=假;工厂BeanName=空;工厂方法名=空;初始化方法名=空; destroyMethodName=null' 具有相同的 'order' 值。 使用自定义过滤器时,请确保位置不与默认过滤器冲突。或者您可以通过删除相应的子元素并避免使用来禁用默认过滤器。

【问题讨论】:

    标签: java spring spring-security


    【解决方案1】:

    我是通过手动编写所需的自动配置 bean 来完成的。结果如下:

    <!-- HTTP security configurations -->
    <security:http auto-config="false" use-expressions="true" entry-point-ref="loginUrlAuthenticationEntryPoint">
    
        <!--
        <security:form-login login-processing-url="/resources/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t" />
            replaced by lowerCaseUsernamePasswordAuthenticationFilter
            the custom-filter with position FORM_LOGIN_FILTER requries that auto-config is false!
         -->
        <security:custom-filter ref="lowerCaseUsernamePasswordAuthenticationFilter" position="FORM_LOGIN_FILTER"/>
        <security:logout logout-url="/resources/j_spring_security_logout" />
    
        <security:intercept-url pattern="/**" access="isAuthenticated()" />
    </security:http>
    
    <bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
        <property name="loginFormUrl" value="/login"/>
    </bean>
    
    <bean id="lowerCaseUsernamePasswordAuthenticationFilter"
        class="com.queomedia.cfma.infrastructure.security.LowerCaseUsernamePasswordAuthenticationFilter">
        <property name="filterProcessesUrl" value="/resources/j_spring_security_check"/>
        <property name="authenticationManager" ref="authenticationManager"/>
        <property name="authenticationFailureHandler">
            <bean class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
                <property name="defaultFailureUrl" value="/login?login_error=t"/>       
            </bean>
        </property>
    </bean>
    

    【讨论】:

      【解决方案2】:

      这是 Scala 中的一个示例。我必须这样做才能替换 Spring Security OAuth 提供的过滤器。

      基本上这个想法是,将FilterChainProxy 和要替换的现有过滤器注入过滤器。在filterChainMap 中找到现有的过滤器并将其替换为您的。

      import org.springframework.security.oauth2.provider.verification.{VerificationCodeFilter => SpringVerificationCodeFilter}
      
      @Component
      class VerificationCodeFilter extends SpringVerificationCodeFilter with InitializingBean {
        @Autowired var filterChainProxy: FilterChainProxy = _
        @Autowired var springVerificationCodeFilter: SpringVerificationCodeFilter = _
      
      
        override def afterPropertiesSet() {
          super.afterPropertiesSet()
      
          val filterChainMap = filterChainProxy.getFilterChainMap
          val filterChain = 
             filterChainMap.find(_._2.exists(_.isInstanceOf[SpringVerificationCodeFilter])).
                getOrElse(throw new Exception("Could not find VerificationCodeFilter in FilterChainMap"))._2
          val index = filterChain.indexOf(springVerificationCodeFilter)
          filterChain.remove(index)
          filterChain.add(index, this)
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-04
        • 2016-09-08
        • 2012-03-30
        • 2013-07-23
        • 1970-01-01
        • 2019-02-16
        相关资源
        最近更新 更多