如果最终使它起作用。(主要感谢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相关的机制将无法正常工作。



