gpt4 book ai didi

Grails spring security userDetailService 导入服务

转载 作者:行者123 更新时间:2023-12-02 15:17:18 25 4
gpt4 key购买 nike

嗨,无论如何要将服务导入自定义 UserDetailsS​​ervice 吗?我有

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 定义中,启用 Autowiring 并添加对您尝试注入(inject)的服务的引用。

userDetailsService(CustomUserDetailsService) {
it.autowire = true
apiService = ref('apiService')
}

关于Grails spring security userDetailService 导入服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52432985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com