我制作了一个自定义UserDetailsService
,它使登录用户名中的用户名不敏感,并且它正常工作,但是,所有控制器(之前都在工作)现在为authenticatedUser
返回null
在请求authenticatedUser
之前,isLoggedIn()
返回true,getPrincipal()
返回登录用户。
这是自定义UserDetailsService
:
package com.mydomain
import grails.plugin.springsecurity.SpringSecurityUtils
import org.springframework.security.core.authority.GrantedAuthorityImpl
import grails.plugin.springsecurity.userdetails.GrailsUser
import grails.plugin.springsecurity.userdetails.GrailsUserDetailsService
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UsernameNotFoundException
class CaseInsensitiveUserDetailsService implements GrailsUserDetailsService {
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean hasRoles) throws UsernameNotFoundException {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Person.withTransaction { status ->
Person user = Person.findByEmailIlike(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, user.id)
}
}
}