From dc4700a13c776bf75521584abede385bac7c686e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=82=E6=A2=A6=E6=8A=80=E6=9C=AF?= <596392912@qq.com> Date: Fri, 28 May 2021 16:41:45 +0800 Subject: [PATCH] :sparkles: pig-common-core Code optimization. --- .../core/config/RedisTemplateConfiguration.java | 6 +----- .../datasource/config/DataSourceProperties.java | 2 -- .../pig/common/feign/ext/PigSentinelFeign.java | 10 +++++----- .../feign/ext/PigSentinelInvocationHandler.java | 12 +++++------- .../pig4cloud/pig/common/log/util/SysLogUtils.java | 3 ++- .../mybatis/resolver/SqlFilterArgumentResolver.java | 2 +- .../security/component/PigAccessDeniedHandler.java | 4 ++-- .../security/component/PigBearerTokenExtractor.java | 4 ++-- ...PigResourceServerTokenRelayAutoConfiguration.java | 2 +- .../component/ResourceAuthExceptionEntryPoint.java | 4 ++-- 10 files changed, 21 insertions(+), 28 deletions(-) diff --git a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/RedisTemplateConfiguration.java b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/RedisTemplateConfiguration.java index 93b2ae67..10cb84dd 100755 --- a/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/RedisTemplateConfiguration.java +++ b/pig-common/pig-common-core/src/main/java/com/pig4cloud/pig/common/core/config/RedisTemplateConfiguration.java @@ -16,7 +16,6 @@ package com.pig4cloud.pig.common.core.config; -import lombok.RequiredArgsConstructor; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.cache.annotation.EnableCaching; @@ -33,14 +32,11 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; */ @EnableCaching @Configuration(proxyBeanMethods = false) -@RequiredArgsConstructor @AutoConfigureBefore(RedisAutoConfiguration.class) public class RedisTemplateConfiguration { - private final RedisConnectionFactory factory; - @Bean - public RedisTemplate redisTemplate() { + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { RedisTemplate redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); diff --git a/pig-common/pig-common-datasource/src/main/java/com/pig4cloud/pig/common/datasource/config/DataSourceProperties.java b/pig-common/pig-common-datasource/src/main/java/com/pig4cloud/pig/common/datasource/config/DataSourceProperties.java index 9dbc1494..3da42e4f 100644 --- a/pig-common/pig-common-datasource/src/main/java/com/pig4cloud/pig/common/datasource/config/DataSourceProperties.java +++ b/pig-common/pig-common-datasource/src/main/java/com/pig4cloud/pig/common/datasource/config/DataSourceProperties.java @@ -18,7 +18,6 @@ package com.pig4cloud.pig.common.datasource.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; /** * @author lengleng @@ -26,7 +25,6 @@ import org.springframework.stereotype.Component; *

*/ @Data -@Component @ConfigurationProperties("spring.datasource") public class DataSourceProperties { diff --git a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java index f5d56aed..bf6bb6c4 100644 --- a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java +++ b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelFeign.java @@ -79,8 +79,8 @@ public final class PigSentinelFeign { // 查找 FeignClient 上的 降级策略 FeignClient feignClient = AnnotationUtils.findAnnotation(target.type(), FeignClient.class); - Class fallback = feignClient.fallback(); - Class fallbackFactory = feignClient.fallbackFactory(); + Class fallback = feignClient.fallback(); + Class fallbackFactory = feignClient.fallbackFactory(); String beanName = feignClient.contextId(); if (!StringUtils.hasText(beanName)) { @@ -88,7 +88,7 @@ public final class PigSentinelFeign { } Object fallbackInstance; - FallbackFactory fallbackFactoryInstance; + FallbackFactory fallbackFactoryInstance; if (void.class != fallback) { fallbackInstance = getFromContext(beanName, "fallback", fallback, target.type()); return new PigSentinelInvocationHandler(target, dispatch, @@ -96,14 +96,14 @@ public final class PigSentinelFeign { } if (void.class != fallbackFactory) { - fallbackFactoryInstance = (FallbackFactory) getFromContext(beanName, "fallbackFactory", + fallbackFactoryInstance = (FallbackFactory) getFromContext(beanName, "fallbackFactory", fallbackFactory, FallbackFactory.class); return new PigSentinelInvocationHandler(target, dispatch, fallbackFactoryInstance); } return new PigSentinelInvocationHandler(target, dispatch); } - private Object getFromContext(String name, String type, Class fallbackType, Class targetType) { + private Object getFromContext(String name, String type, Class fallbackType, Class targetType) { Object fallbackInstance = feignContext.getInstance(name, fallbackType); if (fallbackInstance == null) { throw new IllegalStateException(String.format( diff --git a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelInvocationHandler.java b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelInvocationHandler.java index 031f0968..ae2d99d6 100644 --- a/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelInvocationHandler.java +++ b/pig-common/pig-common-feign/src/main/java/com/pig4cloud/pig/common/feign/ext/PigSentinelInvocationHandler.java @@ -60,12 +60,12 @@ public class PigSentinelInvocationHandler implements InvocationHandler { private final Map dispatch; - private FallbackFactory fallbackFactory; + private FallbackFactory fallbackFactory; private Map fallbackMethodMap; PigSentinelInvocationHandler(Target target, Map dispatch, - FallbackFactory fallbackFactory) { + FallbackFactory fallbackFactory) { this.target = checkNotNull(target, "target"); this.dispatch = checkNotNull(dispatch, "dispatch"); this.fallbackFactory = fallbackFactory; @@ -99,7 +99,7 @@ public class PigSentinelInvocationHandler implements InvocationHandler { InvocationHandlerFactory.MethodHandler methodHandler = this.dispatch.get(method); // only handle by HardCodedTarget if (target instanceof Target.HardCodedTarget) { - Target.HardCodedTarget hardCodedTarget = (Target.HardCodedTarget) target; + Target.HardCodedTarget hardCodedTarget = (Target.HardCodedTarget) target; MethodMetadata methodMetadata = SentinelContractHolder.METADATA_MAP .get(hardCodedTarget.type().getName() + Feign.configKey(hardCodedTarget.type(), method)); // resource default is HttpMethod:protocol://url @@ -107,7 +107,7 @@ public class PigSentinelInvocationHandler implements InvocationHandler { result = methodHandler.invoke(args); } else { - String resourceName = methodMetadata.template().method().toUpperCase() + ":" + hardCodedTarget.url() + String resourceName = methodMetadata.template().method().toUpperCase() + ':' + hardCodedTarget.url() + methodMetadata.template().path(); Entry entry = null; try { @@ -122,9 +122,7 @@ public class PigSentinelInvocationHandler implements InvocationHandler { } if (fallbackFactory != null) { try { - Object fallbackResult = fallbackMethodMap.get(method).invoke(fallbackFactory.create(ex), - args); - return fallbackResult; + return fallbackMethodMap.get(method).invoke(fallbackFactory.create(ex), args); } catch (IllegalAccessException e) { // shouldn't happen as method is public due to being an diff --git a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java index 9d9aa5b6..b9992db3 100755 --- a/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java +++ b/pig-common/pig-common-log/src/main/java/com/pig4cloud/pig/common/log/util/SysLogUtils.java @@ -21,6 +21,7 @@ import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.http.HttpUtil; import com.pig4cloud.pig.admin.api.entity.SysLog; import lombok.experimental.UtilityClass; +import org.springframework.http.HttpHeaders; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.provider.OAuth2Authentication; @@ -47,7 +48,7 @@ public class SysLogUtils { sysLog.setRemoteAddr(ServletUtil.getClientIP(request)); sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI())); sysLog.setMethod(request.getMethod()); - sysLog.setUserAgent(request.getHeader("user-agent")); + sysLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT)); sysLog.setParams(HttpUtil.toParams(request.getParameterMap())); sysLog.setServiceId(getClientId()); return sysLog; diff --git a/pig-common/pig-common-mybatis/src/main/java/com/pig4cloud/pig/common/mybatis/resolver/SqlFilterArgumentResolver.java b/pig-common/pig-common-mybatis/src/main/java/com/pig4cloud/pig/common/mybatis/resolver/SqlFilterArgumentResolver.java index 1bc37597..c27bc942 100644 --- a/pig-common/pig-common-mybatis/src/main/java/com/pig4cloud/pig/common/mybatis/resolver/SqlFilterArgumentResolver.java +++ b/pig-common/pig-common-mybatis/src/main/java/com/pig4cloud/pig/common/mybatis/resolver/SqlFilterArgumentResolver.java @@ -78,7 +78,7 @@ public class SqlFilterArgumentResolver implements HandlerMethodArgumentResolver String current = request.getParameter("current"); String size = request.getParameter("size"); - Page page = new Page(); + Page page = new Page<>(); if (StrUtil.isNotBlank(current)) { page.setCurrent(Long.parseLong(current)); } diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java index 1454fde0..d99e9a64 100755 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigAccessDeniedHandler.java @@ -29,9 +29,9 @@ import com.pig4cloud.pig.common.core.util.R; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; import org.springframework.security.access.AccessDeniedException; import org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler; -import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -41,7 +41,7 @@ import java.io.PrintWriter; * @author lengleng 授权拒绝处理器,覆盖默认的OAuth2AccessDeniedHandler 包装失败信息到PigDeniedException */ @Slf4j -@Component +@Configuration(proxyBeanMethods = false) @RequiredArgsConstructor public class PigAccessDeniedHandler extends OAuth2AccessDeniedHandler { diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigBearerTokenExtractor.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigBearerTokenExtractor.java index ecfc26e5..0595f947 100644 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigBearerTokenExtractor.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigBearerTokenExtractor.java @@ -17,9 +17,9 @@ package com.pig4cloud.pig.common.security.component; import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Configuration; import org.springframework.security.core.Authentication; import org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor; -import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; @@ -31,7 +31,7 @@ import javax.servlet.http.HttpServletRequest; * @author caiqy * @date 2020.05.15 */ -@Component +@Configuration(proxyBeanMethods = false) @RequiredArgsConstructor public class PigBearerTokenExtractor extends BearerTokenExtractor { diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerTokenRelayAutoConfiguration.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerTokenRelayAutoConfiguration.java index 27eb7e13..58f65424 100755 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerTokenRelayAutoConfiguration.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/PigResourceServerTokenRelayAutoConfiguration.java @@ -36,7 +36,7 @@ import java.lang.annotation.*; * @author lengleng * @date 2019/2/1 注入AccessTokenContextRelay 解决feign 传递token 为空问题 */ -@Configuration +@Configuration(proxyBeanMethods = false) @AutoConfigureAfter(OAuth2AutoConfiguration.class) @ConditionalOnWebApplication @ConditionalOnProperty("security.oauth2.client.client-id") diff --git a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java index 04808d8a..de6967fb 100755 --- a/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java +++ b/pig-common/pig-common-security/src/main/java/com/pig4cloud/pig/common/security/component/ResourceAuthExceptionEntryPoint.java @@ -23,9 +23,9 @@ import com.pig4cloud.pig.common.core.util.R; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Configuration; import org.springframework.security.core.AuthenticationException; import org.springframework.security.web.AuthenticationEntryPoint; -import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -36,7 +36,7 @@ import java.io.PrintWriter; * @date 2019/2/1 客户端异常处理 1. 可以根据 AuthenticationException 不同细化异常处理 */ @Slf4j -@Component +@Configuration(proxyBeanMethods = false) @RequiredArgsConstructor public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint {