2010-07-08 102 views
4

我有問題。在Struts中,我有一個處理用戶身份驗證的操作,即,我獲取了用戶的憑據並使用DAO來驗證用戶憑據。我想在Spring中保持相同的設置。我正在使用Spring 3.0.3 RELEASE。春季定製認證

我的問題是,我讀過Spring Security,並在那裏指定JDBC後端「驗證」提供程序。我想知道,如果用戶點擊「登錄」,它會將憑據提交給我的控制器以檢查有效的身份驗證?

我想這樣做的原因是我有一個處理用戶身份驗證和授權的服務。

在此先感謝。

PS如何在Spring中使某些控制器安全?
PPS我是新來的春天

回答

2

你可以寫你自己的Spring Security驗證機制。它必須由以下部分組成:

  • 驗證過濾器 - 讀取請求的數據,然後調用驗證提供商與憑證令牌(類驗證的實例)
  • 驗證提供商 - 接受該身份驗證令牌(過濾器可以創建不同的tokents,並且每個令牌類型都可以有不同的auth提供者),並嘗試進行身份驗證(根據您的情況調用您的服務)。負載從什麼地方在用戶細節簽署了(從JDBC,其他服務等)
+0

好的自定義的認證供應商,眼見我一個新手@春天,我該怎麼做? – 2010-07-08 16:36:06

+0

@精英:這不是你的情況。 – axtavt 2010-07-08 17:12:19

+0

@axtavt:Elite表示他需要使用單獨的服務對用戶進行身份驗證。如果這是真的,那麼他必須至少寫自己的Auth Provider。或者我誤解了一些東西? – 2010-07-08 21:54:21

3

一般 - 身份驗證你可能(或可能不會)調用用戶信息服務或填寫的所有用戶數據在那裏

  • 用戶詳細信息售後服務Spring Security在自己的代碼中處理認證,使用代碼作爲策略(認證提供者,用戶詳細信息服務等)。但是你可以在你自己的代碼中處理認證。

    在動作的代碼,當用戶憑據是正確的,你會:

    • 創建Authentication包含用戶名和授予的角色(你可以使用UsernamePasswordAuthenticationToken作爲一種方便的實現)。
    • 把它放進安全上下文:
      SecurityContextHolder.getContext().setAuthentication(auth);
    • 廣播使用AuthenticationEventPublisher.publishAuthenticationSuccess(...)認證成功事件(你可以從上下文自動裝配,或創建一個DefaultAuthenticationEventPublisher明確)。
    • 使用SavedRequestAwareAuthenticationSuccessHandler.onAuthenticationSuccess(...)將用戶重定向到安全資源。

    你也需要提供一個AuthenticationEntryPoint

    <bean id = "aep" class = "org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
        <!-- Your login page --> 
        <property name = "loginFormUrl" value = "/login" /> 
    </bean> 
    
    <security:http entry-point-ref="aep"> 
        ... 
    </http> 
    

    但是,如果你在春天實際上新的,它可能是更好的避免這樣的大規模定製和使用普通Spring Security的架構。

  • 5

    您可以創建一個實現org.springframework.security.authentication.AuthenticationProvider這樣

    package com.bzone.example; 
    
    import org.springframework.security.authentication.AuthenticationProvider; 
    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; 
    import org.springframework.security.core.Authentication; 
    import org.springframework.security.core.AuthenticationException; 
    
    
    public class CustomAuthenticationProvider implements AuthenticationProvider{ 
    
        @Override 
        public Authentication authenticate(Authentication authentication) 
          throws AuthenticationException { 
         // TODO call custom service or do whatever you want 
         return null; 
        } 
    
        @Override 
        public boolean supports(Class<? extends Object> authentication) { 
         // copied it from AbstractUserDetailsAuthenticationProvider 
         return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication)); 
        } 
    
    } 
    

    一個步驟是配置Spring Security使用這個定製的身份驗證提供

    <?xml version="1.0" encoding="UTF-8"?> 
    <beans:beans xmlns="http://www.springframework.org/schema/security" 
        xmlns:beans="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
             http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd"> 
    
        <!-- HTTP security configurations --> 
        <http auto-config="true" use-expressions="true"> 
         <form-login login-processing-url="/static/j_spring_security_check" login-page="/login" authentication-failure-url="/login?login_error=t"/> 
         <logout logout-url="/static/j_spring_security_logout"/> 
    
         <!-- Configure these elements to secure URIs in your application --> 
         <intercept-url pattern="/member/**" access="isAuthenticated()" /> 
         <intercept-url pattern="/resources/**" access="permitAll" /> 
         <intercept-url pattern="/static/**" access="permitAll" /> 
         <intercept-url pattern="/**" access="permitAll" /> 
        </http> 
    
        <!-- Configure Authentication mechanism --> 
        <authentication-manager alias="authenticationManager"> 
         <authentication-provider ref="com.bzone.example.CustomAuthenticationProvider" /> 
        </authentication-manager> 
    
    </beans:beans> 
    
    +0

    糾正我,如果我錯了,但我認爲這立即在重寫'authenticate()'方法,而不是在控制器中。 – Oneb 2013-03-25 09:16:58