在Grails的spring安全插件中使用电子邮件和用户名作为登录名

在Grails的spring安全插件中使用电子邮件和用户名作为登录名,grails,spring-security,Grails,Spring Security,我在我的Grails应用程序中使用spring安全插件,我希望注册的用户在注册过程中使用用户名或电子邮件地址登录应用程序。(就像fb一样) 我已经查看了web和Grails文档: 但没有找到任何解决办法 任何帮助都将不胜感激 提前感谢从spring安全的角度来看,您应该实现自己的org.springframework.security.core.userdetails.userdetails服务 在spring配置中,您必须引用UserDetails类: <authentication-

我在我的Grails应用程序中使用spring安全插件,我希望注册的用户在注册过程中使用用户名或电子邮件地址登录应用程序。(就像fb一样)

我已经查看了web和Grails文档:

但没有找到任何解决办法

任何帮助都将不胜感激


提前感谢

从spring安全的角度来看,您应该实现自己的org.springframework.security.core.userdetails.userdetails服务

在spring配置中,您必须引用UserDetails类:

<authentication-manager>
    <authentication-provider user-service-ref="myUserServiceImpl" />
</authentication-manager>


hth

我不认为这是最近的事。在SpringSecurity/Grails3.2.8的更高版本中。这是我的:

这是在服务中创建的服务。用户电子邮件位于用户之外的一个单独的类中,因此具有
属性。电子邮件
检查,否则您只需执行
User.findByUsernameOrEmail(用户名,用户名)

因此,我将UserEmailUserService作为一项服务:

package com.app.users

import grails.plugin.springsecurity.userdetails.GormUserDetailsService
import grails.transaction.Transactional
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.context.SecurityContextHolder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException

class UserEmailUserService extends GormUserDetailsService{

    UserDetails loadUserByUsername(String username, boolean loadRoles)
            throws UsernameNotFoundException {
        return loadUserByUsername(username)
    }

    @Transactional
    UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //enable login with either username or password
        User user = User.find {
            username == username || attributes.email == username
        }
        if (!user) throw new UsernameNotFoundException('User not found', username)
        UserDetails userDetails = new org.springframework.security.core.userdetails.User(user.username, user.getPassword(),
                user.enabled, !user.accountExpired, !user.passwordExpired, !user.accountLocked, getAuthorities(user.roles))
        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities())
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return userDetails
    }

    public static List<GrantedAuthority> getAuthorities(Set<Role> roles) {
       List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>()
       roles?.each { role ->
          authorities.add(new SimpleGrantedAuthority(role.authority))
       }
       return authorities
   }
}
请注意,我调用了
user.roles
这是我在用户类中进行的自定义调用。由于我有团队角色等:

Set<Role> getRoles() {
    UserRole.findAllByUser(this)*.role
}
Set getRoles(){
UserRole.findallbyser(此)*.role
}
import com.app.users.UserEmailUserService

// Place your Spring DSL code here
beans = {
      userDetailsService(UserEmailUserService){
            grailsApplication = ref('grailsApplication')
        }
}
Set<Role> getRoles() {
    UserRole.findAllByUser(this)*.role
}