2015-01-21 92 views
1

我需要檢查用戶是否已通過生效日期未過期登錄之前。我應該怎麼做?我有兩個自定義authenticationsuccesshandlerauthenticationfailurehandler春季安全檢查生效日期和過期日期登錄之前

<form-login login-page="/login" 
    authentication-failure-url="/login?error" 
    authentication-failure-handler-ref="authenticationFailureHandler" 
    authentication-success-handler-ref="authenticationSuccessHandlerWithoutReferer1"/> 

彈簧security.xml文件

<authentication-manager> 
     <authentication-provider> 
     <jdbc-user-service data-source-ref="dataSource" 
          users-by-username-query="select USER_ID,USER_PWD,USER_STATUS from USER where USER_ID=?" 
          authorities-by-username-query="select username, authority from authorities where username =? " /> 
     <password-encoder hash="md5"/> 
    </authentication-provider> 
</authentication-manager> 

回答

1

您需要執行UserDetailsService接口。

@Service("customUserService") 
public class CustomUserService implements UserDetailsService { 

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 
     // find out all necessary information about the user 
     // for example, use JdbcTemplate to query from your data source 
     // note especially the boolean accountNonExpired below 
     return new User(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); 
    } 
} 

您不能使用jdbc-user-service標籤。相反:

<authentication-manager> 
    <authentication-provider user-service-ref="customUserService"> 
    <!-- password encoder etc --> 
    </authentication-provider> 
</authentication-manager> 
+0

docs告訴你不要在'userDetailsS​​ervice'中進行認證,雖然看起來更簡單,但正確的方法是使用'customAuthenticationProvider'進行自定義認證處理 – mariubog 2015-01-22 22:26:27

+0

@ user1289300檢查帳戶是否過期未進行認證。定製認證提供者是更多(更難)的工作。 – holmis83 2015-01-22 22:38:43

+0

@ holmis83你是對的。因爲我是春季安保的新手。順便說一下,'AuthenticationFailureHandler'會與'UserDetailsS​​ervice'一起崩潰嗎? – 2015-01-23 01:06:46

0

如果你需要根據你要實現的AuthenticationProvider

http://docs.spring.io/spring-security/site/docs/4.0.0.RC1/reference/htmlsingle/#tech-userdetailsservice

然後將其添加到您的AuthenticationManagerBuilder如果文檔的一些特殊情況下認證你正在使用javaconfig。 如果使用元數據,我認爲你需要做這樣的事情:

<authentication-manager> 
    <authentication-provider ref="customAuthenticationProvider"/> 
</authentication-manager> 
+0

我可以只添加''到我的xml嗎? (檢查編輯) – 2015-01-21 08:01:52

+0

我真的不記得什麼最好的方式添加它在元數據,但重點是你必須實現自定義身份驗證提供程序,並提供它作爲您的優先認證提供商 – mariubog 2015-01-21 18:48:56

-1

創建一個自定義的UserDetailsS​​ervice實現

例子:

@Service("customUserService") 
public class UserServiceImpl implements UserDetailsService{ 

    /** 
    * Used by spring security 
    */ 
    public UserDetails loadUserByUsername(String username) 
     throws UsernameNotFoundException { 

     try{ 
     //Do all your check 
     //create object of User and return, password can be anything 
     return new User(username, password, authorities); 
     }catch(NubeException e){ 
     throw new UsernameNotFoundException("user_not_found"); 
     } 
    } 

} 

然後告訴Spring使用你的類來驗證:

<authentication-manager> 
    <authentication-provider user-service-ref="customUserService" /> 
</authentication-manager> 
+0

所以我不能再使用我的數據源? (檢查編輯) – 2015-01-21 06:04:51

+0

它是黑客而不是正確的方式做到這一點,文件提到不要這樣做,並讓detailsS​​ervice保持純粹的DAO – mariubog 2015-01-21 14:52:08

+0

密碼不能爲空! – holmis83 2015-01-22 22:17:32

相關問題