您可以使用自定义来执行此操作UserDetailsService
- 请参阅https://grails-plugins.github.io/grails-spring-security-core/v3/index.html#userDetailsService
package com.yourcompany.yourapp
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUser
import org.codehaus.groovy.grails.plugins.springsecurity.GrailsUserDetailsService
import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils
import org.springframework.security.core.authority.GrantedAuthorityImpl
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
class MyUserDetailsService implements GrailsUserDetailsService {
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User.withTransaction { status ->
User user = User.findByUsernameIlike(username)
if (!user) throw new UsernameNotFoundException('User not found', username)
def authorities = user.authorities.collect { new GrantedAuthorityImpl(it.authority) }
new GrailsUser(user.username, user.password, user.enabled, !user.accountExpired, !user.passwordExpired,
!user.accountLocked, authorities ?: NO_ROLES, user.id)
}
}
UserDetails loadUserByUsername(String username, boolean loadRoles) throws UsernameNotFoundException {
loadUserByUsername username
}
}
在 grails-app/conf/spring/resources.groovy 中注册它以覆盖插件的实现:
beans = {
userDetailsService(com.yourcompany.yourapp.MyUserDetailsService)
}