2012-09-17 80 views
1

我只想問是否可以使用其他屬性,但不包括用戶名&用於登錄的域類的密碼?Grails Spring Security - 使用其他屬性進行身份驗證

例如我有一個Person域類,它與Account域類有one-to-one的關係。我想用Person域類中的firstName & birthDate屬性對我的用戶進行身份驗證。

有沒有一種配置可以讓我做到這一點?

回答

2

是的。這是可能的。

您必須編寫一個自定義的authentication-provider,其中您可以採用您喜歡的參數進行身份驗證。

舉個例子,你可能有這樣的事情:

public class MyUserDetailsService implements UserDetailsService{ 
    @Override 
    public UserDetails loadUserByUsername(String username){ 
     //You have to override this method to have your desired action 
     //If those criteria are not satisfied you may return null 
     //Otherwise have an UserDetails filled and returned 
     org.springframework.security.core.userdetails.User userDetails = new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.getIsActive(), true, true, true, getAuthorities(user)); 
     return userDetails 

    } 

而在你的bean配置,使用自己的authentication-provider這樣的:

<sec:authentication-manager alias="authenticationManager"> 
    <sec:authentication-provider user-service-ref="myUserDetailsService"/> 
</sec:authentication-manager> 
相關問題