springboot的config文件通常如下:
spring.data.jdbc.url=jdbc:mysql://127.0.0.1:3305/test_db
spring.data.jdbc.username=root
spring.data.jdbc.password=123456
通常會將敏感信息加密,一般的解決方案會在config的bean中進行邏輯解密代碼的處理,但是不夠優美,這裏介紹一種更好的思路和方案,拿現有的一個實現jasypt。
項目地址:https://github.com/ulisesbocchio/jasypt-spring-boot
使用步驟:
1、springboot項目引入依賴,使用@AutoConfiguration
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2、生成密文
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=password algorithm=PBEWithMD5AndDES
input的值就是原密碼,password的值就是參數jasypt.encryptor.password指定的值,即祕鑰。
3、最後一步,將明文密碼替換爲ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==)
可以使用自定義的前綴和後綴取代ENC(),通過配置包裹密文的前後綴:
jasypt.encryptor.property.prefix=ENC@[ jasypt.encryptor.property.suffix=]
部分核心源碼如下:
public class EnableEncryptablePropertiesBeanFactoryPostProcessor implements BeanFactoryPostProcessor, ApplicationListener<ApplicationEvent>, Ordered {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
// 得到加密字符串的處理類(已經加密的密碼通過它來解密)
EncryptablePropertyResolver propertyResolver = beanFactory.getBean(RESOLVER_BEAN_NAME, EncryptablePropertyResolver.class);
// springboot下的Environment裏包含了所有我們定義的屬性, 也就包含了application.properties中所有的屬性
MutablePropertySources propSources = environment.getPropertySources();
// 核心,PropertySource的getProperty(String)方法委託給EncryptablePropertySourceWrapper
convertPropertySources(interceptionMode, propertyResolver, propSources);
}
@Override
public int getOrder() {
// 讓這個jasypt定義的BeanFactoryPostProcessor的初始化順序最低,即最後初始化
return Ordered.LOWEST_PRECEDENCE;
}
}
PropertySource的getProperty(String)方法委託給EncryptablePropertySourceWrapper,那麼當獲取屬性時,實際上就是調用EncryptablePropertySourceWrapper的getProperty()方法,在這個方法裏我們就能對value進行解密了。
EncryptablePropertySourceWrapper實現了接口EncryptablePropertyResolver,該定義如下:
// An interface to resolve property values that may be encrypted.
public interface EncryptablePropertyResolver {
String resolvePropertyValue(String value);
}
接口描述:
Returns the unencrypted version of the value provided free on any prefixes/suffixes or any other metadata surrounding the encrypted value. Or the actual same String if no encryption was detected.
如果通過prefixes/suffixes包裹的屬性,那麼返回解密後的值;
如果沒有被包裹,那麼返回原生的值;
實現類的實現如下:
@Override
public String resolvePropertyValue(String value) {
String actualValue = value;
// 如果value是加密的value,則進行解密。
if (detector.isEncrypted(value)) {
try {
// 解密算法核心實現
actualValue = encryptor.decrypt(detector.unwrapEncryptedValue(value.trim()));
} catch (EncryptionOperationNotPossibleException e) {
// 如果解密失敗,那麼拋出異常。
throw new DecryptionException("Decryption of Properties failed, make sure encryption/decryption passwords match", e);
}
}
// 沒有加密的value,返回原生value即可
return actualValue;
}
判斷是否是加密的邏輯很簡單:(trimmedValue.startsWith(prefix) && trimmedValue.endsWith(suffix))
,即只要value是以prefixe/suffixe包括,就認爲是加密的value。
通過對源碼的分析可知jasypt的原理很簡單,就是講原本spring中PropertySource的getProperty(String)方法委託給我們自定義的實現。然後再自定義實現中,判斷value是否是已經加密的value,如果是,則進行解密。如果不是,則返回原value。