栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

处理Spring Boot外部化的属性值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

处理Spring Boot外部化的属性值

如果最终使它起作用。(主要感谢github上的stephane-deraco)

解决方案的关键是要实现的类

ApplicationContextInitializer<ConfigurableApplicationContext>
。我叫它
PropertyPasswordDecodingContextInitializer

主要的问题是让弹簧来使用它

ApplicationContextInitializer
。重要信息可以在参考资料中找到。我使用 meta-INF /
spring.factories选择
了以下内容的方法:

org.springframework.context.ApplicationContextInitializer=ch.mycompany.myproject.PropertyPasswordDecodingContextInitializer

PropertyPasswordDecodingContextInitializer
使用
PropertyPasswordDeprer
和实现类,目前为简单起见
base64PropertyPasswordDeprer

PropertyPasswordDecodingContextInitializer.java

package ch.mycompany.myproject;import java.util.linkedHashMap;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.springframework.context.ApplicationContextInitializer;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.core.env.CompositePropertySource;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.EnumerablePropertySource;import org.springframework.core.env.MapPropertySource;import org.springframework.core.env.PropertySource;import org.springframework.stereotype.Component;@Componentpublic class PropertyPasswordDecodingContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {    private static final Pattern deprePasswordPattern = Pattern.compile("password\((.*?)\)");    private PropertyPasswordDeprer passwordDeprer = new base64PropertyPasswordDeprer();    @Override    public void initialize(ConfigurableApplicationContext applicationContext) {        ConfigurableEnvironment environment = applicationContext.getEnvironment();        for (PropertySource<?> propertySource : environment.getPropertySources()) { Map<String, Object> propertyOverrides = new linkedHashMap<>(); deprePasswords(propertySource, propertyOverrides); if (!propertyOverrides.isEmpty()) {     PropertySource<?> depredProperties = new MapPropertySource("depred "+ propertySource.getName(), propertyOverrides);     environment.getPropertySources().addBefore(propertySource.getName(), depredProperties); }        }    }    private void deprePasswords(PropertySource<?> source, Map<String, Object> propertyOverrides) {        if (source instanceof EnumerablePropertySource) { EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) source; for (String key : enumerablePropertySource.getPropertyNames()) {     Object rawValue = source.getProperty(key);     if (rawValue instanceof String) {         String depredValue = deprePasswordsInString((String) rawValue);         propertyOverrides.put(key, depredValue);     } }        }    }    private String deprePasswordsInString(String input) {        if (input == null) return null;        StringBuffer output = new StringBuffer();        Matcher matcher = deprePasswordPattern.matcher(input);        while (matcher.find()) { String replacement = passwordDeprer.deprePassword(matcher.group(1)); matcher.appendReplacement(output, replacement);        }        matcher.appendTail(output);        return output.toString();    }}

PropertyPasswordDeprer.java

package ch.mycompany.myproject;public interface PropertyPasswordDeprer {    public String deprePassword(String enpredPassword);}

base64PropertyPasswordDeprer.java

package ch.mycompany.myproject;import java.io.UnsupportedEncodingException;import org.apache.commons.prec.binary.base64;public class base64PropertyPasswordDeprer implements PropertyPasswordDeprer {    @Override    public String deprePassword(String enpredPassword) {        try { byte[] depredData = base64.deprebase64(enpredPassword); String depredString = new String(depredData, "UTF-8"); return depredString;        } catch (UnsupportedEncodingException e) { throw new RuntimeException(e);        }    }}

请注意,ApplicationContext在此阶段尚未完成初始化,因此自动装配或任何其他与Bean相关的机制将无法正常工作。




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/381901.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号