From 66296afd26b9b7dbe85e2594e4f5a8402d994d9a Mon Sep 17 00:00:00 2001 From: lbw Date: Wed, 30 Nov 2022 20:56:43 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96sas=20=E5=BC=82=E5=B8=B8=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PigAuthenticationFailureEventHandler.java | 14 +++++++++++--- .../PigRemoteRegisteredClientRepository.java | 6 ++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationFailureEventHandler.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationFailureEventHandler.java index 9ef45843..a12b2a99 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationFailureEventHandler.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationFailureEventHandler.java @@ -33,6 +33,7 @@ import org.springframework.http.MediaType; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.server.ServletServerHttpResponse; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.oauth2.core.OAuth2AuthenticationException; import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; import org.springframework.security.web.authentication.AuthenticationFailureHandler; @@ -87,14 +88,21 @@ public class PigAuthenticationFailureEventHandler implements AuthenticationFailu httpResponse.setStatusCode(HttpStatus.UNAUTHORIZED); String errorMessage; + if (exception instanceof OAuth2AuthenticationException) { + OAuth2AuthenticationException authorizationException = (OAuth2AuthenticationException) exception; + errorMessage = StrUtil.isBlank(authorizationException.getError().getDescription()) + ? authorizationException.getError().getErrorCode() + : authorizationException.getError().getDescription(); + } + else { + errorMessage = exception.getLocalizedMessage(); + } + // 手机号登录 String grantType = request.getParameter(OAuth2ParameterNames.GRANT_TYPE); if (SecurityConstants.APP.equals(grantType)) { errorMessage = MsgUtils.getSecurityMessage("AbstractUserDetailsAuthenticationProvider.smsBadCredentials"); } - else { - errorMessage = exception.getLocalizedMessage(); - } this.errorHttpResponseConverter.write(R.failed(errorMessage), MediaType.APPLICATION_JSON, httpResponse); } diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java index 1464eba4..ee44bae1 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/service/PigRemoteRegisteredClientRepository.java @@ -7,12 +7,13 @@ import com.pig4cloud.pig.admin.api.feign.RemoteClientDetailsService; import com.pig4cloud.pig.common.core.constant.CacheConstants; import com.pig4cloud.pig.common.core.constant.SecurityConstants; import com.pig4cloud.pig.common.core.util.RetOps; -import com.pig4cloud.pig.common.security.util.OAuthClientException; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import org.springframework.cache.annotation.Cacheable; import org.springframework.security.oauth2.core.AuthorizationGrantType; import org.springframework.security.oauth2.core.ClientAuthenticationMethod; +import org.springframework.security.oauth2.core.OAuth2Error; +import org.springframework.security.oauth2.server.authorization.authentication.OAuth2AuthorizationCodeRequestAuthenticationException; import org.springframework.security.oauth2.server.authorization.client.RegisteredClient; import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository; import org.springframework.security.oauth2.server.authorization.settings.ClientSettings; @@ -87,7 +88,8 @@ public class PigRemoteRegisteredClientRepository implements RegisteredClientRepo public RegisteredClient findByClientId(String clientId) { SysOauthClientDetails clientDetails = RetOps.of(clientDetailsService.getClientDetailsById(clientId)).getData() - .orElseThrow(() -> new OAuthClientException("客户端查询异常,请检查数据库链接")); + .orElseThrow(() -> new OAuth2AuthorizationCodeRequestAuthenticationException( + new OAuth2Error("客户端查询异常,请检查数据库链接"), null)); RegisteredClient.Builder builder = RegisteredClient.withId(clientDetails.getClientId()) .clientId(clientDetails.getClientId())