pig-common-security Code optimization remove @ComponentScan.

This commit is contained in:
如梦技术 2021-05-28 17:20:44 +08:00
parent dc4700a13c
commit c1fc58bade
9 changed files with 33 additions and 26 deletions

View File

@ -17,7 +17,9 @@
package com.pig4cloud.pig.common.security.annotation;
import com.pig4cloud.pig.common.security.component.PigResourceServerAutoConfiguration;
import com.pig4cloud.pig.common.security.component.PigResourceServerTokenRelayAutoConfiguration;
import com.pig4cloud.pig.common.security.component.PigSecurityBeanDefinitionRegistrar;
import com.pig4cloud.pig.common.security.feign.PigFeignClientConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
@ -36,7 +38,8 @@ import java.lang.annotation.*;
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Import({ PigResourceServerAutoConfiguration.class, PigSecurityBeanDefinitionRegistrar.class })
@Import({ PigResourceServerAutoConfiguration.class, PigSecurityBeanDefinitionRegistrar.class,
PigResourceServerTokenRelayAutoConfiguration.class, PigFeignClientConfiguration.class })
public @interface EnablePigResourceServer {
}

View File

@ -17,11 +17,9 @@
package com.pig4cloud.pig.common.security.component;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils;
@ -31,8 +29,6 @@ import java.util.Collection;
* @author lengleng
* @date 2019/2/1 接口权限判断工具
*/
@Slf4j
@Component("pms")
public class PermissionService {
/**

View File

@ -27,7 +27,6 @@ import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
@ -46,7 +45,6 @@ import java.util.regex.Pattern;
* 资源服务器对外直接暴露URL,如果设置contex-path 要特殊处理
*/
@Slf4j
@Configuration(proxyBeanMethods = false)
@ConfigurationProperties(prefix = "security.oauth2.ignore")
public class PermitAllUrlProperties implements InitializingBean, ApplicationContextAware {

View File

@ -29,7 +29,6 @@ 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;
@ -41,7 +40,6 @@ import java.io.PrintWriter;
* @author lengleng 授权拒绝处理器覆盖默认的OAuth2AccessDeniedHandler 包装失败信息到PigDeniedException
*/
@Slf4j
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class PigAccessDeniedHandler extends OAuth2AccessDeniedHandler {

View File

@ -16,8 +16,6 @@
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.util.AntPathMatcher;
@ -31,14 +29,17 @@ import javax.servlet.http.HttpServletRequest;
* @author caiqy
* @date 2020.05.15
*/
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class PigBearerTokenExtractor extends BearerTokenExtractor {
private final PathMatcher pathMatcher = new AntPathMatcher();
private final PathMatcher pathMatcher;
private final PermitAllUrlProperties urlProperties;
public PigBearerTokenExtractor(PermitAllUrlProperties urlProperties) {
this.urlProperties = urlProperties;
this.pathMatcher = new AntPathMatcher();
}
@Override
public Authentication extract(HttpServletRequest request) {
boolean match = urlProperties.getUrls().stream()

View File

@ -16,11 +16,11 @@
package com.pig4cloud.pig.common.security.component;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@ -35,10 +35,29 @@ import java.util.Collections;
* @author lengleng
* @date 2020-06-23
*/
@ConfigurationPropertiesScan
@ComponentScan("com.pig4cloud.pig.common.security")
@EnableConfigurationProperties(PermitAllUrlProperties.class)
public class PigResourceServerAutoConfiguration {
@Bean("pms")
public PermissionService permissionService() {
return new PermissionService();
}
@Bean
public PigAccessDeniedHandler pigAccessDeniedHandler(ObjectMapper objectMapper) {
return new PigAccessDeniedHandler(objectMapper);
}
@Bean
public PigBearerTokenExtractor pigBearerTokenExtractor(PermitAllUrlProperties urlProperties) {
return new PigBearerTokenExtractor(urlProperties);
}
@Bean
public ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint(ObjectMapper objectMapper) {
return new ResourceAuthExceptionEntryPoint(objectMapper);
}
@Bean
@Primary
@LoadBalanced

View File

@ -25,7 +25,6 @@ import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfigur
import org.springframework.cloud.commons.security.AccessTokenContextRelay;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.config.annotation.web.configuration.OAuth2ClientConfiguration;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
@ -36,7 +35,6 @@ import java.lang.annotation.*;
* @author lengleng
* @date 2019/2/1 注入AccessTokenContextRelay 解决feign 传递token 为空问题
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(OAuth2AutoConfiguration.class)
@ConditionalOnWebApplication
@ConditionalOnProperty("security.oauth2.client.client-id")

View File

@ -22,8 +22,6 @@ import com.pig4cloud.pig.common.core.constant.CommonConstants;
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;
@ -35,8 +33,6 @@ import java.io.PrintWriter;
* @author lengleng
* @date 2019/2/1 客户端异常处理 1. 可以根据 AuthenticationException 不同细化异常处理
*/
@Slf4j
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
public class ResourceAuthExceptionEntryPoint implements AuthenticationEntryPoint {

View File

@ -20,7 +20,6 @@ import feign.RequestInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.commons.security.AccessTokenContextRelay;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2ClientContext;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
@ -28,7 +27,6 @@ import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResour
* @author lengleng
* @date 2019/2/1 feign 拦截器传递 header 中oauth token 使用hystrix 的信号量模式
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("security.oauth2.client.client-id")
public class PigFeignClientConfiguration {