簡(jiǎn)介從Spring Boot 1.3開(kāi)始,我們可以在應(yīng)用程序上下文刷新之前使用 文章持續(xù)更新,微信搜索「萬(wàn)貓學(xué)社」第一時(shí)間閱讀,關(guān)注后回復(fù)「電子書(shū)」,免費(fèi)獲取12本Java必讀技術(shù)書(shū)籍。 使用示例讓我們?cè)O(shè)想一個(gè)需求,配置文件中的數(shù)據(jù)庫(kù)密碼是加密后的密文,如: spring.datasource.password=js8sbAwkduzPTEWQrlDbTw== 在應(yīng)用啟動(dòng)時(shí),對(duì)密文進(jìn)行解密后再進(jìn)行數(shù)據(jù)庫(kù)的連接。 針對(duì)這種需求,就可以通過(guò) 文章持續(xù)更新,微信搜索「萬(wàn)貓學(xué)社」第一時(shí)間閱讀,關(guān)注后回復(fù)「電子書(shū)」,免費(fèi)獲取12本Java必讀技術(shù)書(shū)籍。 1.實(shí)現(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.注冊(cè)實(shí)現(xiàn)類(lèi)想要在Spring Boot啟動(dòng)過(guò)程中調(diào)用這個(gè)實(shí)現(xiàn)類(lèi),我們還需要在 org.springframework.boot.env.EnvironmentPostProcessor= one.more.DecodeEnvironmentPostProcessor 文章持續(xù)更新,微信搜索「萬(wàn)貓學(xué)社」第一時(shí)間閱讀,關(guān)注后回復(fù)「電子書(shū)」,免費(fèi)獲取12本Java必讀技術(shù)書(shū)籍。 單元測(cè)試下面介紹本文的重點(diǎn):怎么做 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)); } /** * 獲取一個(gè)經(jīng)過(guò)EnvironmentPostProcessor處理過(guò)的Environment * * @param processor EnvironmentPostProcessor實(shí)現(xiàn)類(lèi)的實(shí)例 * @param properties 預(yù)置準(zhǔn)備做單元測(cè)試的屬性 * @return 處理過(guò)的Environment */ private ConfigurableEnvironment getEnvironment(EnvironmentPostProcessor processor, Properties properties) { // 創(chuàng)建一個(gè)SpringApplication SpringApplication springApplication = new SpringApplicationBuilder() .sources(DecodeEnvironmentPostProcessor.class) .web(WebApplicationType.NONE).build(); // 獲取應(yīng)用上下文 ConfigurableApplicationContext context = springApplication.run(); // 獲取Environment ConfigurableEnvironment environment = context.getEnvironment(); //添加準(zhǔn)備做單元測(cè)試的屬性 environment.getPropertySources() .addFirst(new PropertiesPropertySource("test", properties)); processor.postProcessEnvironment(environment, springApplication); context.close(); return environment; } } 文章持續(xù)更新,微信搜索「萬(wàn)貓學(xué)社」第一時(shí)間閱讀,關(guān)注后回復(fù)「電子書(shū)」,免費(fèi)獲取12本Java必讀技術(shù)書(shū)籍。 附:加解密工具類(lèi)代碼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; } } |
|
來(lái)自: 路人甲Java > 《待分類(lèi)》