From f0db0492fef6e6422d9747f3629db7a58e459320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=95=8F?= Date: Sat, 20 Jan 2024 04:29:10 +0800 Subject: [PATCH] use GrailsUserDetailsService get authorities. (#25) * use GrailsUserDetailsService get authorities. * Update SpringSecurityOauth2BaseService.groovy Remove commented code --------- Co-authored-by: chenmin25 Co-authored-by: Puneet Behl --- .../SpringSecurityOauth2BaseService.groovy | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/grails-app/services/grails/plugin/springsecurity/oauth2/SpringSecurityOauth2BaseService.groovy b/grails-app/services/grails/plugin/springsecurity/oauth2/SpringSecurityOauth2BaseService.groovy index 50c70fe..939adad 100644 --- a/grails-app/services/grails/plugin/springsecurity/oauth2/SpringSecurityOauth2BaseService.groovy +++ b/grails-app/services/grails/plugin/springsecurity/oauth2/SpringSecurityOauth2BaseService.groovy @@ -25,26 +25,29 @@ import grails.plugin.springsecurity.oauth2.token.OAuth2SpringToken import grails.plugin.springsecurity.oauth2.util.OAuth2ProviderConfiguration import grails.plugin.springsecurity.userdetails.GormUserDetailsService import grails.plugin.springsecurity.userdetails.GrailsUser +import grails.plugin.springsecurity.userdetails.GrailsUserDetailsService import groovy.util.logging.Slf4j import org.apache.commons.lang.exception.ExceptionUtils import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.security.core.AuthenticationException -import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.userdetails.UserDetails @Transactional @Slf4j class SpringSecurityOauth2BaseService { -/** - * Map for storing the different OAuth2Provider - */ + /** + * Map for storing the different OAuth2Provider + */ Map providerServiceMap = new HashMap<>() private Map _providerConfigurationMap = new HashMap<>() GrailsApplication grailsApplication AuthenticationManager authenticationManager + GrailsUserDetailsService userDetailsService + OAuth2SpringToken createAuthToken(String providerName, OAuth2AccessToken scribeToken) { def providerService = getProviderService(providerName) OAuth2SpringToken oAuthToken = providerService.createSpringAuthToken(scribeToken) @@ -108,11 +111,8 @@ class SpringSecurityOauth2BaseService { boolean passwordExpired = passwordExpiredPropertyName ? user."${passwordExpiredPropertyName}" : false // authorities - - String authoritiesPropertyName = conf.userLookup.authoritiesPropertyName - String authorityPropertyName = conf.authority.nameField - Collection userAuthorities = user."${authoritiesPropertyName}" - def authorities = userAuthorities.collect { new SimpleGrantedAuthority(it."${authorityPropertyName}") } + UserDetails userDetails = userDetailsService.loadUserByUsername(username,true) + def authorities= userDetails.authorities oAuthToken.principal = new GrailsUser(username, password, enabled, !accountExpired, !passwordExpired, !accountLocked, authorities ?: [GormUserDetailsService.NO_ROLE], user.id)