我只想询问是否可以使用除username和amp;之外的其他属性。用于登录的域类的密码?
例如,我有一个Person
域类与one-to-one
域类有Account
的关系。我想使用他们的firstName
&在birthDate
域类中找到Person
个属性。
我是否可以采取配置来实现这一目标?
答案 0 :(得分: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>