pig-common-core Code optimization.

This commit is contained in:
如梦技术 2021-05-28 16:41:45 +08:00
parent c92ef06cd8
commit dc4700a13c
10 changed files with 21 additions and 28 deletions

View File

@ -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<String, Object> redisTemplate() {
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());

View File

@ -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;
* <p>
*/
@Data
@Component
@ConfigurationProperties("spring.datasource")
public class DataSourceProperties {

View File

@ -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(

View File

@ -60,12 +60,12 @@ public class PigSentinelInvocationHandler implements InvocationHandler {
private final Map<Method, InvocationHandlerFactory.MethodHandler> dispatch;
private FallbackFactory fallbackFactory;
private FallbackFactory<?> fallbackFactory;
private Map<Method, Method> fallbackMethodMap;
PigSentinelInvocationHandler(Target<?> target, Map<Method, InvocationHandlerFactory.MethodHandler> 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

View File

@ -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;

View File

@ -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));
}

View File

@ -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 {

View File

@ -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 {

View File

@ -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")

View File

@ -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 {