♻️ 重构代码,注解条件注入 参考 @feign实现,重构@pigxresoureserver

This commit is contained in:
冷冷 committed 2018-11-24 17:51:54 +08:00
1 parent c981e6137d
commit 2dbc9377b2
6 files changed
+69 -33

No files matched your search

-12
View File
@@ -1,14 +1,3 @@
### 使用说明
请参考VIP群共享 《从零开始部署pigx》
[>> 文档](doc/md/catalog.md)
视频 | ppt
---|---
[>> 环境准备、运行pigx](https://www.bilibili.com/video/av33200189)| [PPT](https://slides.com/lengleng/pigx-springcloud-6/fullscreen#/)
[>> pigx定时任务、运维平台使用](https://www.bilibili.com/video/av33268288)| [PPT](https://slides.com/lengleng/pigx-springcloud-7/fullscreen#/)
[>> pigx 配置文件加解密原理和选择](https://www.bilibili.com/video/av33315412)| [PPT](https://slides.com/lengleng/pigx-springcloud-6-8/fullscreen#/)
## 协议和授权
pigX并非一个开源软件,作者保留全部的权利。
@@ -22,7 +11,6 @@ pigX并非一个开源软件,作者保留全部的权利。
1. 将本项目的部分或全部代码和资源进行任何形式的再发行(尤其上传GitHub、Gitee )
2. 利用本项目的部分或全部代码和资源进行`任何商业行为`
3. 举报有奖,抢先体验`pigx 2.0`。
## 贡献代码
@@ -111,4 +111,9 @@ public interface SecurityConstants {
*/
String DEFAULT_SELECT_STATEMENT = BASE_FIND_STATEMENT + " where client_id = ?";
/**
* 资源服务器默认bean名称
*/
String RESOURCE_SERVER_CONFIGURER = "resourceServerConfigurerAdapter";
}
@@ -18,6 +18,7 @@
package com.pig4cloud.pigx.common.security.annotation;
import com.pig4cloud.pigx.common.security.component.PigxResourceServerAutoConfiguration;
import com.pig4cloud.pigx.common.security.component.PigxSecurityBeanDefinitionRegistrar;
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 +37,7 @@ import java.lang.annotation.*;
@Inherited
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
@Import(PigxResourceServerAutoConfiguration.class)
@Import({PigxResourceServerAutoConfiguration.class, PigxSecurityBeanDefinitionRegistrar.class})
public @interface EnablePigxResourceServer {
/**
@@ -17,21 +17,16 @@
package com.pig4cloud.pigx.common.security.component;
import cn.hutool.core.util.ClassUtil;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.Set;
/**
* @author lengleng
@@ -54,18 +49,4 @@ public class PigxResourceServerAutoConfiguration {
});
return restTemplate;
}
@Bean
@ConditionalOnMissingBean(ResourceServerConfigurerAdapter.class)
public ResourceServerConfigurerAdapter resourceServerConfigurerAdapter() {
Set<Class<?>> classes = ClassUtil.scanPackageByAnnotation("com.pig4cloud.pigx", EnablePigxResourceServer.class);
boolean details = false;
for (Class<?> clazz : classes) {
details = clazz.getAnnotation(EnablePigxResourceServer.class).details();
}
BaseResourceServerConfigurerAdapter baseResourceServerConfigurerAdapter
= new BaseResourceServerConfigurerAdapter();
baseResourceServerConfigurerAdapter.setDetails(details);
return baseResourceServerConfigurerAdapter;
}
}
@@ -38,7 +38,7 @@ import org.springframework.web.client.RestTemplate;
* 2. 支持 获取用户全部信息
*/
@Slf4j
public class BaseResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
public class PigxResourceServerConfigurerAdapter extends ResourceServerConfigurerAdapter {
@Autowired
protected ResourceAuthExceptionEntryPoint resourceAuthExceptionEntryPoint;
@Autowired
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.common.security.component;
import com.pig4cloud.pigx.common.core.constant.SecurityConstants;
import com.pig4cloud.pigx.common.security.annotation.EnablePigxResourceServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import java.util.Map;
/**
* @author lengleng
* @date 2018-11-24
*/
@Slf4j
public class PigxSecurityBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
/**
* 根据注解值动态注入资源服务器的相关属性
*
* @param metadata 注解信息
* @param registry 注册器
*/
@Override
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) {
if (registry.isBeanNameInUse(SecurityConstants.RESOURCE_SERVER_CONFIGURER)) {
log.warn("本地存在资源服务器配置,覆盖默认配置:" + SecurityConstants.RESOURCE_SERVER_CONFIGURER);
return;
}
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(PigxResourceServerConfigurerAdapter.class);
MutablePropertyValues mpv = new MutablePropertyValues();
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(
EnablePigxResourceServer.class.getName());
Object details = annotationAttributes.get("details");
mpv.add("details", details);
beanDefinition.setPropertyValues(mpv);
registry.registerBeanDefinition(SecurityConstants.RESOURCE_SERVER_CONFIGURER, beanDefinition);
}
}