WangHan
2025-04-02 a8ba678a3fe5a39da2c732014cebbb66e408e97c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.iplatform.core.config.enc;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.env.OriginTrackedMapPropertySource;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
 
/**
 * 测试,实现配置文件自动加解密。
 * <pre>
 *     1)本来准备使用:jasypt-spring-boot-starter,但由于其依赖的springboot版本是2.7,所以无法使用;
 *     2)考虑到后续springBoot的频繁升级问题要连带修改,所以决定系统内部模仿实现一个简单配置。
 * </pre>
 * @author 时克英
 * @date 2023-12-04
 */
public class PropertySourcePostProcessor implements BeanFactoryPostProcessor, Ordered {
 
    protected final transient Logger logger = LoggerFactory.getLogger(getClass());
 
    private ConfigurableEnvironment environment;
 
    public PropertySourcePostProcessor(ConfigurableEnvironment environment) {
        this.environment = environment;
    }
 
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // 从ConfigurableEnvironment中取出所有的配置数据
        MutablePropertySources propertySources = this.environment.getPropertySources();
//        propertySources.stream()
//                // 过滤不需要包装的对象
////                .filter(s -> !noWrapPropertySource(s))
//                .filter(s -> {
//                    if(s instanceof MapPropertySource){
//                        return true;
//                    } else {
//                        return false;
//                    }
//                })
//                // 包装所有的PropertySource
//                .map(s -> new EncryptPropertySource((MapPropertySource)s))
//                .collect(Collectors.toList())
//                // 替换掉propertySources中的PropertySource
//                .forEach(wrap -> propertySources.replace(wrap.getName(), wrap));
//
//        propertySources.stream().forEach(propertySource -> {
//            logger.debug("......", propertySource);
//            logger.debug("{} -> {}", propertySource.getName(), propertySource.getSource());
//            if(propertySource instanceof MapPropertySource){
//                logger.debug("发现一个:MapPropertySource {}", propertySource.getClass().getName());
//            }
//            this.decryptPropertyValue(propertySource);
//        });
        for (PropertySource<?> propertySource : propertySources) {
            if (propertySource instanceof OriginTrackedMapPropertySource) {
                propertySources.replace(propertySource.getName(), new PropertySourceWrapper(propertySource
                                , new EncryptionWrapperDetector("ENC(", ")"))
                );
            }
        }
    }
 
//    private void decryptPropertyValue(PropertySource<?> propertySource){
//        Object source = propertySource.getSource();
//        logger.debug("propertySource.getSource = {}", source.getClass().getName());
//        if(source instanceof Map<?,?>){
//            Map<String, Object> properties = (Map<String, Object>)source;
//            logger.debug("map: 共有 {} 个属性", properties.size());
//            for(Map.Entry<String, Object> entry : properties.entrySet()){
//                logger.debug("key = {}, class = {}", entry.getKey(), entry.getValue().getClass().getName());
//            }
//        }
//    }
 
    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE - 100;
    }
//    private boolean noWrapPropertySource(PropertySource propertySource) {
//        return propertySource instanceof EncryptPropertySource || StringUtils.equalsAny(propertySource.getClass().getName(), "org.springframework.core.env.PropertySource$StubPropertySource", "org.springframework.boot.context.properties.source.ConfigurationPropertySourcesPropertySource");
//    }
}