Grails spring security userDetailService 导入服务
Grails spring security userDetailService import service
您好,是否可以将服务导入自定义 UserDetailsService?我有
的错误
org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot invoke method validatePin() on null object
class CustomUserDetailsService implements GrailsUserDetailsService {
def apiService
/**
* Some Spring Security classes (e.g. RoleHierarchyVoter) expect at least
* one role, so we give a user with no granted roles this one which gets
* past that restriction but doesn't grant anything.
*/
static final List NO_ROLES = [new SimpleGrantedAuthority(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles)
throws UsernameNotFoundException {
return loadUserByUsername(username)
}
@Transactional(readOnly=true, noRollbackFor=[IllegalArgumentException, UsernameNotFoundException])
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
GrailsWebRequest webUtils = WebUtils.retrieveGrailsWebRequest()
def request = webUtils.getCurrentRequest()
def countryCode = request.getParameter('country_code')
def pin = request.getParameter('password')
def apiUser = apiService.validatePin(countryCode, username, pin)
println("\n\napiUser: ${apiUser}")
User user = User.findByUsername(username)
if (!user) throw new NoStackUsernameNotFoundException()
def roles = user.authorities
// or if you are using role groups:
// def roles = user.authorities.collect { it.authorities }.flatten().unique()
def authorities = roles.collect {
new SimpleGrantedAuthority(it.authority)
}
return new CustomUserDetails(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired,
!user.accountLocked, authorities ?: NO_ROLES, user.id,
user.firstName + " " + user.lastName)
}
}
推测您正在 resources.groovy
中注册您的自定义用户详细信息服务。在您的 bean 定义中,启用自动装配并添加对您尝试注入的服务的引用。
userDetailsService(CustomUserDetailsService) {
it.autowire = true
apiService = ref('apiService')
}
您好,是否可以将服务导入自定义 UserDetailsService?我有
的错误org.springframework.security.authentication.InternalAuthenticationServiceException: Cannot invoke method validatePin() on null object
class CustomUserDetailsService implements GrailsUserDetailsService {
def apiService
/**
* Some Spring Security classes (e.g. RoleHierarchyVoter) expect at least
* one role, so we give a user with no granted roles this one which gets
* past that restriction but doesn't grant anything.
*/
static final List NO_ROLES = [new SimpleGrantedAuthority(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles)
throws UsernameNotFoundException {
return loadUserByUsername(username)
}
@Transactional(readOnly=true, noRollbackFor=[IllegalArgumentException, UsernameNotFoundException])
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
GrailsWebRequest webUtils = WebUtils.retrieveGrailsWebRequest()
def request = webUtils.getCurrentRequest()
def countryCode = request.getParameter('country_code')
def pin = request.getParameter('password')
def apiUser = apiService.validatePin(countryCode, username, pin)
println("\n\napiUser: ${apiUser}")
User user = User.findByUsername(username)
if (!user) throw new NoStackUsernameNotFoundException()
def roles = user.authorities
// or if you are using role groups:
// def roles = user.authorities.collect { it.authorities }.flatten().unique()
def authorities = roles.collect {
new SimpleGrantedAuthority(it.authority)
}
return new CustomUserDetails(user.username, user.password, user.enabled,
!user.accountExpired, !user.passwordExpired,
!user.accountLocked, authorities ?: NO_ROLES, user.id,
user.firstName + " " + user.lastName)
}
}
推测您正在 resources.groovy
中注册您的自定义用户详细信息服务。在您的 bean 定义中,启用自动装配并添加对您尝试注入的服务的引用。
userDetailsService(CustomUserDetailsService) {
it.autowire = true
apiService = ref('apiService')
}