♻️ Refactoring code. 细节优化,(ossendpoint 展示、常量参数工具、oauth server 异常国际化问题)
This commit is contained in:
1 parent
a646f3599a
commit
be60059bea
4 files changed
+18
-11
No files matched your search
+8
-6
@@ -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> T checkAndGet(String key, Object... defaultVal) {
|
||||
private <T> T checkAndGet(String key, Class<T> 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;
|
||||
}
|
||||
|
||||
+1
-1
@@ -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);
|
||||
}
|
||||
|
||||
+2
-2
@@ -37,11 +37,11 @@ import java.util.Map;
|
||||
* @author lengleng
|
||||
* @author 858695266
|
||||
* <p>
|
||||
* oss.enable
|
||||
* oss.info
|
||||
*/
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("/aws")
|
||||
@RequestMapping("/oss")
|
||||
public class OssEndpoint {
|
||||
|
||||
private final OssTemplate template;
|
||||
|
||||
+7
-2
@@ -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
|
||||
|
||||
Reference in new issue
Block a user