簡介從Spring Boot 1.3開始,我們可以在應用程序上下文刷新之前使用 文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。 使用示例讓我們設想一個需求,配置文件中的數(shù)據(jù)庫密碼是加密后的密文,如: spring.datasource.password=js8sbAwkduzPTEWQrlDbTw== 在應用啟動時,對密文進行解密后再進行數(shù)據(jù)庫的連接。 針對這種需求,就可以通過 文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。 1.實現(xiàn)EnvironmentPostProcessorpackage one.more;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import java.util.Properties;
public class DecodeEnvironmentPostProcessor implements EnvironmentPostProcessor {
public static final String SPRING_DATASOURCE_PASSWORD = "spring.datasource.password";
public static final String AES_SECRET = "OneMore";
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String password = environment.getProperty(SPRING_DATASOURCE_PASSWORD);
Properties properties = new Properties();
properties.setProperty(SPRING_DATASOURCE_PASSWORD, AESUtil.decrypt(password, AES_SECRET));
PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource(SPRING_DATASOURCE_PASSWORD,
properties);
environment.getPropertySources().addFirst(propertiesPropertySource);
}
}如果你希望 2.注冊實現(xiàn)類想要在Spring Boot啟動過程中調用這個實現(xiàn)類,我們還需要在 org.springframework.boot.env.EnvironmentPostProcessor= one.more.DecodeEnvironmentPostProcessor 文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。 單元測試下面介紹本文的重點:怎么做 package one.more;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertiesPropertySource;
import java.util.Properties;
public class DecodeEnvironmentPostProcessorTest {
@Test
public void testPostProcessEnvironment() {
DecodeEnvironmentPostProcessor processor = new DecodeEnvironmentPostProcessor();
String password = "one-more";
Properties properties = new Properties();
properties.setProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD,
AESUtil.encrypt(password, DecodeEnvironmentPostProcessor.AES_SECRET));
ConfigurableEnvironment environment = getEnvironment(processor, properties);
Assert.assertEquals(password,
environment.getProperty(DecodeEnvironmentPostProcessor.SPRING_DATASOURCE_PASSWORD));
}
/**
* 獲取一個經過EnvironmentPostProcessor處理過的Environment
*
* @param processor EnvironmentPostProcessor實現(xiàn)類的實例
* @param properties 預置準備做單元測試的屬性
* @return 處理過的Environment
*/
private ConfigurableEnvironment getEnvironment(EnvironmentPostProcessor processor, Properties properties) {
// 創(chuàng)建一個SpringApplication
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(DecodeEnvironmentPostProcessor.class)
.web(WebApplicationType.NONE).build();
// 獲取應用上下文
ConfigurableApplicationContext context = springApplication.run();
// 獲取Environment
ConfigurableEnvironment environment = context.getEnvironment();
//添加準備做單元測試的屬性
environment.getPropertySources()
.addFirst(new PropertiesPropertySource("test", properties));
processor.postProcessEnvironment(environment, springApplication);
context.close();
return environment;
}
}文章持續(xù)更新,微信搜索「萬貓學社」第一時間閱讀,關注后回復「電子書」,免費獲取12本Java必讀技術書籍。 附:加解密工具類代碼package one.more;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class AESUtil {
private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
private static final String KEY_ALGORITHM = "AES";
public static String encrypt(String content, String password) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
byte[] result = cipher.doFinal(byteContent);
return Base64.encodeBase64String(result);
} catch (Exception ex) {
}
return null;
}
public static String decrypt(String content, String password) {
try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
byte[] result = cipher.doFinal(Base64.decodeBase64(content));
return new String(result, "utf-8");
} catch (Exception ex) {
}
return null;
}
private static SecretKeySpec getSecretKey(final String password) {
KeyGenerator kg = null;
try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
kg.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
} catch (NoSuchAlgorithmException ex) {
}
return null;
}
}
|
|
|