2011-10-11 13 views
14

मैंने अपना खुद का LowerCaseUsernamePasswordAuthenticationFilter लागू किया है जो कि UsernamePasswordAuthenticationFilter का उप-वर्ग है।कस्टम उपयोगकर्ता नाम का उपयोग करने के लिए स्प्रिंग सुरक्षा कॉन्फ़िगर करें पासवर्ड उपयोगकर्ता प्रमाणीकरणफिल्टर

लेकिन अब मेरी समस्या यह है कि इस फ़िल्टर का उपयोग करने के लिए स्प्रिंग सुरक्षा को कॉन्फ़िगर कैसे करें।

अब मैं प्रयोग किया जाता करने के लिए:

<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 = झूठी; autowireMode = 0; dependencyCheck = 0; autowireCandidate = true प्राथमिक = झूठी; factoryBeanName = बातिल; factoryMethodName = बातिल; initMethodName = बातिल; नष्ट करें विधि_नाम = शून्य 'एक ही' ऑर्डर 'मान है। कस्टम फ़िल्टर का उपयोग करते समय, कृपया सुनिश्चित करें कि स्थिति डिफ़ॉल्ट फ़िल्टर के साथ संघर्ष नहीं करती है। वैकल्पिक रूप से आप संबंधित बाल तत्वों को हटाकर और उपयोग से परहेज करके डिफ़ॉल्ट फ़िल्टर अक्षम कर सकते हैं।

उत्तर

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

यहां स्कैला में एक उदाहरण दिया गया है। मुझे स्प्रिंग सिक्योरिटी ओएथ द्वारा प्रदान किए गए फ़िल्टर को प्रतिस्थापित करने के लिए ऐसा करना पड़ा।

असल में विचार है, 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) 
    } 
} 
संबंधित मुद्दे