!350 当获取token参数grant_type不为password时,鉴权接口一直报‘用户不存在’错误

Merge pull request !350 from 贾同学/dev
This commit is contained in:
lbw 2023-03-24 01:39:07 +00:00 committed by Gitee
commit 1781ea5833
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -111,29 +111,29 @@ public abstract class OAuth2ResourceOwnerBaseAuthenticationProvider<T extends OA
@Override @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { public Authentication authenticate(Authentication authentication) throws AuthenticationException {
T resouceOwnerBaseAuthentication = (T) authentication; T resourceOwnerBaseAuthentication = (T) authentication;
OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient( OAuth2ClientAuthenticationToken clientPrincipal = getAuthenticatedClientElseThrowInvalidClient(
resouceOwnerBaseAuthentication); resourceOwnerBaseAuthentication);
RegisteredClient registeredClient = clientPrincipal.getRegisteredClient(); RegisteredClient registeredClient = clientPrincipal.getRegisteredClient();
checkClient(registeredClient); checkClient(registeredClient);
Set<String> authorizedScopes; Set<String> authorizedScopes;
// Default to configured scopes // Default to configured scopes
if (!CollectionUtils.isEmpty(resouceOwnerBaseAuthentication.getScopes())) { if (!CollectionUtils.isEmpty(resourceOwnerBaseAuthentication.getScopes())) {
for (String requestedScope : resouceOwnerBaseAuthentication.getScopes()) { for (String requestedScope : resourceOwnerBaseAuthentication.getScopes()) {
if (!registeredClient.getScopes().contains(requestedScope)) { if (!registeredClient.getScopes().contains(requestedScope)) {
throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_SCOPE); throw new OAuth2AuthenticationException(OAuth2ErrorCodes.INVALID_SCOPE);
} }
} }
authorizedScopes = new LinkedHashSet<>(resouceOwnerBaseAuthentication.getScopes()); authorizedScopes = new LinkedHashSet<>(resourceOwnerBaseAuthentication.getScopes());
} }
else { else {
throw new ScopeException(OAuth2ErrorCodesExpand.SCOPE_IS_EMPTY); throw new ScopeException(OAuth2ErrorCodesExpand.SCOPE_IS_EMPTY);
} }
Map<String, Object> reqParameters = resouceOwnerBaseAuthentication.getAdditionalParameters(); Map<String, Object> reqParameters = resourceOwnerBaseAuthentication.getAdditionalParameters();
try { try {
UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = buildToken(reqParameters); UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = buildToken(reqParameters);
@ -149,14 +149,14 @@ public abstract class OAuth2ResourceOwnerBaseAuthenticationProvider<T extends OA
.principal(usernamePasswordAuthentication) .principal(usernamePasswordAuthentication)
.authorizationServerContext(AuthorizationServerContextHolder.getContext()) .authorizationServerContext(AuthorizationServerContextHolder.getContext())
.authorizedScopes(authorizedScopes) .authorizedScopes(authorizedScopes)
.authorizationGrantType(AuthorizationGrantType.PASSWORD) .authorizationGrantType(resourceOwnerBaseAuthentication.getAuthorizationGrantType())
.authorizationGrant(resouceOwnerBaseAuthentication); .authorizationGrant(resourceOwnerBaseAuthentication);
// @formatter:on // @formatter:on
OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization OAuth2Authorization.Builder authorizationBuilder = OAuth2Authorization
.withRegisteredClient(registeredClient) .withRegisteredClient(registeredClient)
.principalName(usernamePasswordAuthentication.getName()) .principalName(usernamePasswordAuthentication.getName())
.authorizationGrantType(AuthorizationGrantType.PASSWORD) .authorizationGrantType(resourceOwnerBaseAuthentication.getAuthorizationGrantType())
// 0.4.0 新增的方法 // 0.4.0 新增的方法
.authorizedScopes(authorizedScopes); .authorizedScopes(authorizedScopes);