From be60059bea71693196b06e2e26998c6d88dd7ed1 Mon Sep 17 00:00:00 2001 From: lengleng Date: Sat, 20 Jun 2020 23:05:36 +0800 Subject: [PATCH] =?UTF-8?q?:recycle:=20Refactoring=20code.=20=E7=BB=86?= =?UTF-8?q?=E8=8A=82=E4=BC=98=E5=8C=96=EF=BC=8C=EF=BC=88ossendpoint=20?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E3=80=81=E5=B8=B8=E9=87=8F=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E3=80=81oauth=20server=20=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=9B=BD=E9=99=85=E5=8C=96=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pigx/common/data/resolver/ParamResolver.java | 14 ++++++++------ .../pigx/common/oss/OssAutoConfiguration.java | 2 +- .../pigx/common/oss/http/OssEndpoint.java | 4 ++-- .../PigxWebResponseExceptionTranslator.java | 9 +++++++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/resolver/ParamResolver.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/resolver/ParamResolver.java index 1696dd95e..1a5435462 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/resolver/ParamResolver.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/resolver/ParamResolver.java @@ -1,5 +1,6 @@ package com.pig4cloud.pigx.common.data.resolver; +import cn.hutool.core.convert.Convert; import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.admin.api.feign.RemoteParamService; import com.pig4cloud.pigx.common.core.constant.SecurityConstants; @@ -22,7 +23,7 @@ public class ParamResolver { * @return value */ public Integer getInt(String key, Integer... defaultVal) { - return checkAndGet(key, defaultVal); + return checkAndGet(key, Integer.class, defaultVal); } /** @@ -32,10 +33,10 @@ public class ParamResolver { * @return value */ public String getStr(String key, String... defaultVal) { - return checkAndGet(key, defaultVal); + return checkAndGet(key, String.class, defaultVal); } - private T checkAndGet(String key, Object... defaultVal) { + private T checkAndGet(String key, Class clazz, T... defaultVal) { // 校验入参是否合法 if (StrUtil.isBlank(key) || defaultVal.length > 1) { throw new IllegalArgumentException("参数不合法"); @@ -46,11 +47,12 @@ public class ParamResolver { String result = remoteParamService.getByKey(key, SecurityConstants.FROM_IN).getData(); if (StrUtil.isNotBlank(result)) { - return (T) result; + return Convert.convert(clazz, result); } - if (defaultVal != null) { - return (T) defaultVal.clone()[0]; + if (defaultVal.length == 1) { + return Convert.convert(clazz, defaultVal.clone()[0]); + } return null; } diff --git a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/OssAutoConfiguration.java b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/OssAutoConfiguration.java index 6d569efa8..8df6256e7 100755 --- a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/OssAutoConfiguration.java +++ b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/OssAutoConfiguration.java @@ -45,7 +45,7 @@ public class OssAutoConfiguration { } @Bean - @ConditionalOnProperty(name = "oss.public", havingValue = "true", matchIfMissing = true) + @ConditionalOnProperty(name = "oss.info", havingValue = "true") public OssEndpoint ossEndpoint(OssTemplate template) { return new OssEndpoint(template); } diff --git a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/http/OssEndpoint.java b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/http/OssEndpoint.java index 4b1f911e4..f1e6d0b3c 100755 --- a/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/http/OssEndpoint.java +++ b/pigx-common/pigx-common-oss/src/main/java/com/pig4cloud/pigx/common/oss/http/OssEndpoint.java @@ -37,11 +37,11 @@ import java.util.Map; * @author lengleng * @author 858695266 *

- * oss.enable + * oss.info */ @RestController @AllArgsConstructor -@RequestMapping("/aws") +@RequestMapping("/oss") public class OssEndpoint { private final OssTemplate template; diff --git a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxWebResponseExceptionTranslator.java b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxWebResponseExceptionTranslator.java index 8d1d7f015..324a0a8ed 100644 --- a/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxWebResponseExceptionTranslator.java +++ b/pigx-common/pigx-common-security/src/main/java/com/pig4cloud/pigx/common/security/component/PigxWebResponseExceptionTranslator.java @@ -24,6 +24,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.SpringSecurityMessageSource; import org.springframework.security.oauth2.common.DefaultThrowableAnalyzer; import org.springframework.security.oauth2.common.OAuth2AccessToken; import org.springframework.security.oauth2.common.exceptions.ClientAuthenticationException; @@ -34,9 +35,11 @@ import org.springframework.security.oauth2.provider.error.WebResponseExceptionTr import org.springframework.security.web.util.ThrowableAnalyzer; import org.springframework.web.HttpRequestMethodNotSupportedException; +import java.util.Locale; + /** * @author lengleng - * @date 2018/7/8 异常处理,重写oauth 默认实现 + * @date 2020-06-20 OAutH Server 异常处理,重写oauth 默认实现 */ @Slf4j public class PigxWebResponseExceptionTranslator implements WebResponseExceptionTranslator { @@ -64,7 +67,9 @@ public class PigxWebResponseExceptionTranslator implements WebResponseExceptionT ase = (InvalidGrantException) throwableAnalyzer.getFirstThrowableOfType(InvalidGrantException.class, causeChain); if (ase != null) { - return handleOAuth2Exception(new InvalidException(ase.getMessage(), ase)); + String msg = SpringSecurityMessageSource.getAccessor().getMessage( + "AbstractUserDetailsAuthenticationProvider.badCredentials", ase.getMessage(), Locale.CHINA); + return handleOAuth2Exception(new InvalidException(msg, ase)); } ase = (HttpRequestMethodNotSupportedException) throwableAnalyzer