2011-10-11 9 views
14

のサブクラスであるLowerCaseUsernamePasswordAuthenticationFilterを実装しました。カスタムUsernamePasswordAuthenticationFilterを使用するようにSpringセキュリティを設定する

私の問題は、このフィルタを使用するために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> 

はそのメッセージと例外が発生します:

設定問題:フィルタ豆<lowerCaseUsernamePasswordAuthenticationFilter>と「ルート豆:クラス[ org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter];スコープ=;抽象的な=偽; lazyInit = false; autowireMode = 0; dependencyCheck = 0; autowireCandidate = true;一次=偽; factoryBeanName = null; factoryMethodName = null; initMethodName = null; destroyMethodName = null 'は同じ' order '値を持ちます。 カスタムフィルタを使用する場合は、その位置がデフォルトのフィルタと矛盾していないことを確認してください。また、デフォルトのフィルタを無効にするには、対応する子要素を削除し、その使用を避けます。

答えて

12

私は手で必要な自動設定の豆を書き込むことによってそれを行っています。これが結果です。

<!-- 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) 
    } 
} 
関連する問題