2009-01-15 65 views
21

我無法準確發現需要實現的內容,才能使用Spring Security的Web應用程序使用自定義身份驗證方法。我有一個Spring Security插件的Grails應用程序,它目前使用標準的用戶/密碼認證和瀏覽器表單。這工作正常。使用Acegi/Spring Security創建自定義身份驗證

我需要實現一種機制,並執行一種MAC認證。如果HTTP請求包含多個參數(例如用戶標識符,時間戳,簽名等),則需要採用這些參數,執行一些哈希和簽名/時間戳比較,然後對用戶進行身份驗證。

我不是100%確定從哪裏開始。 Spring Security類需要擴展/實現什麼?我已閱讀Reference Documentation並對概念有了一個很好的理解,但我不確定是否需要過濾器或提供程序或管理器,或者在何處/如何創建Authentication對象。我搞砸了試圖擴展AbstractProcessingFilter和/或實現AuthenticationProvider,但我只是想知道如何讓它們都很好地發揮。

回答

23
  1. 實現自定義AuthenticationProvider它得到來自Authentication所有身份驗證信息:getCredentials()getDetails()getPrincipal()。使用下面的配置片段

    領帶到您的Spring Security認證機制:

<bean id="myAuthenticationProvider" class="com.example.MyAuthenticationProvider"> 
    <security:custom-authentication-provider /> 
</bean> 
  • 這一步是可選的,如果你能找到一個合適的來自標準實施。如果沒有,執行一類擴展Authentication界面上,你可以把你的驗證參數:

    (e.g. a user identifier, timestamp, signature, etc.) 
    
  • 擴展的自定義SpringSecurityFilter使上述2類聯繫在一起。例如,篩選器可能會得到AuthenticationManager,並使用您的Authentication的實現作爲輸入調用authenticate()

    您可以將AbstractAuthenticationProcessingFilter作爲開始。

    您可以參考UsernamePasswordAuthenticationFilter,它延伸AbstractAuthenticationProcessingFilterUsernamePasswordAuthenticationFilter執行標準的用戶名/密碼認證。

  • 配置您的Spring Security以添加或替換標準AUTHENTICATION_PROCESSING_FILTER。對於春季安全過濾訂單,看到http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html#filter-stack

    這裏是如何使用您的實現取代它的配置片斷:

  • <beans:bean id="myFilter" class="com.example.MyAuthenticationFilter"> 
        <custom-filter position="AUTHENTICATION_PROCESSING_FILTER"/> 
    </beans:bean> 
    
    +0

    我想你需要使用PRE_AUTH_FILTER位置而不是AUTHENTICATION_PROCESSING_FILTER。此外,您可能想要查看本教程以瞭解Google AppEngine中的身份驗證的實現:http://blog.springsource.com/2010/08/02/spring-security-in-google-app-engine/ – Tal 2011-09-12 07:29:03

    1

    下面是使用自定義autenticationFilter securityContext.xml配置文件的例子(擴展AUTHENTICATION_PROCESSING_FILTER)和authenticationProvider。用戶認證數據由jdbc連接提供。配置適用於Spring Security 2.0。X

    <?xml version="1.0" encoding="UTF-8"?> 
    
    <sec:global-method-security /> 
    
    <sec:http auto-config="false" realm="CUSTOM" create-session="always" servlet-api-provision="true" 
        entry-point-ref="authenticationProcessingFilterEntryPoint" access-denied-page="/notauthorized.xhtml" 
        session-fixation-protection="migrateSession"> 
        <sec:port-mappings> 
        <sec:port-mapping http="80" https="443" /> 
        </sec:port-mappings> 
    
        <sec:anonymous granted-authority="ROLE_ANONYMOUS" username="Anonymous" /> 
        <sec:intercept-url pattern="/**" access="ROLE_ANONYMOUS, ROLE_USER" /> 
    
        <sec:logout logout-url="/logoff" logout-success-url="/home.xhtml" invalidate-session="false" /> 
    
    </sec:http> 
    
    <bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint"> 
        <property name="loginFormUrl" value="/login.xhtml" /> 
        <property name="forceHttps" value="false" /> 
    </bean> 
    
    <bean id="authenticationProcessingFilter" class="mypackage.CustomAuthenticationProcessingFilter"> 
        <sec:custom-filter position="AUTHENTICATION_PROCESSING_FILTER" /> 
        <property name="defaultTargetUrl" value="/" /> 
        <property name="filterProcessesUrl" value="/logon" /> 
        <property name="authenticationFailureUrl" value="/loginError.xhtml" /> 
        <property name="alwaysUseDefaultTargetUrl" value="false" /> 
        <property name="authenticationManager" ref="authenticationManager" /> 
    </bean> 
    
    <jee:jndi-lookup id="securityDataSource" jndi-name="jdbc/DB_DS" /> 
    
    <bean id="myUserDetailsService" class="mypackage.CustomJdbcDaoImpl"> 
        <property name="dataSource" ref="securityDataSource" /> 
        <property name="rolePrefix" value="ROLE_" /> 
    </bean> 
    
    <bean id="apcAuthenticationProvider" class="mypackage.CustomDaoAuthenticationProvider"> 
        <property name="userDetailsService" ref="myUserDetailsService" /> 
        <sec:custom-authentication-provider /> 
    </bean> 
    
    <bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager"> 
        <property name="providers"> 
        <list> 
        <ref local="apcAuthenticationProvider" /> 
        </list> 
        </property> 
    </bean> 
    
    </beans> 
    
    1

    我最近提出了,做使用Spring Security 3 的源代碼是here自定義驗證的示例應用程序。 更多細節在this blog post

    相關問題