Hi is there anyway to import service onto customUserDetailService? I had error of
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)
}
}
Presumably you are registering your custom user detail service in resources.groovy
. In your bean definition, enable autowiring and add a reference to the service you're trying to inject.
userDetailsService(CustomUserDetailsService) {
it.autowire = true
apiService = ref('apiService')
}