我只想问问是否可以使用除域类的用户名和密码之外的其他属性进行登录?
例如,我有一个Person
域类,该类与one-to-one
域类具有Account
关系。我想使用firstName
域类中的birthDate
和Person
属性对用户进行身份验证。
有没有可以做的配置才能做到这一点?
最佳答案
是。有可能
您必须编写自定义的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>
关于authentication - Grails Spring Security-在身份验证中使用其他属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452642/