圣杯弹簧安全用户详细服务导入服务



嗨,无论如何都可以将服务导入自定义用户详细信息服务吗?我有错误

org.springframework.security.authentication.InternalAuthenticationServiceException: 无法在空对象上调用方法 validatePin((

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("nnapiUser: ${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')
}

最新更新