authentication - Grails Spring Security-在身份验证中使用其他属性

标签 authentication grails spring-security

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

例如,我有一个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>

关于authentication - Grails Spring Security-在身份验证中使用其他属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12452642/

相关文章:

grails - 更新 Grails 中父域类中的 "lastUpdated"字段

grails - 编辑插件的标签库。然后如何将其提交给svn?

java - Spring Security 禁用方法安全性

java - 在 SpringBoot 应用程序中使用 @RolesAllowed 的异常

jsp - 如果已经登录,则从 login.jsp 重定向

php - 使用 Laravel 和 Passport 在身份验证失败时响应状态码 401?

php - 有没有办法强制使用 Zend_Auth 进行身份验证?

web-services - 如何改进 Perl Dancer 应用程序中的身份验证

Grails 在数据库级别实现双向一对一关系

spring-boot - JWT token 安全