我只想问问是否可以使用除域类的用户名和密码之外的其他属性进行登录?

例如,我有一个Person域类,该类与one-to-one域类具有Account关系。我想使用firstName域类中的birthDatePerson属性对用户进行身份验证。

有没有可以做的配置才能做到这一点?

请您参考如下方法:

是。有可能

您必须编写自定义的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> 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!