你的配置檔案是不是還在使用下面這種落後的配置暴露一些密碼:
-
jdbc.url=jdbc:mysql://127.0.0.1:3305/afei
-
jdbc.username=afei
-
jdbc.password=123456
如果是,那麼繼續往下看。筆者今天介紹史上最優雅加密接入方式:jasypt。
使用方式用法一
先看用法有多簡單,以 springboot 為例:
- Application.java 上增加注解 @EnableEncryptableProperties;
- 增加配置檔案 jasypt.encryptor.password = Afei@2018 ,這是加密的秘鑰;
- 所有明文密碼替換為 ENC (加密字元串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==) ;
- 引入一個MAVEN依賴;
maven坐标如下:
-
<dependency>
-
<groupId>com.github.ulisesbocchio</groupId>
-
<artifactId>jasypt-spring-boot</artifactId>
-
<version>2.0.0</version>
-
</dependency>
簡答的 4 步就搞定啦,是不是超簡單?完全不需要修改任何業務代碼。其中第三步的加密字元串的生成方式為:
-
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=Afei@2018 algorithm=PBEWithMD5AndDES
其中:
- input的值就是原密碼。
- password的值就是參數jasypt.encryptor.password指定的值,即秘鑰。
用法二
其實還有另一種更簡單的姿勢:
- 增加配置檔案 jasypt.encryptor.password = Afei@2018,這是加密的秘鑰;
- 所有明文密碼替換為 ENC (加密字元串),例如 ENC(XW2daxuaTftQ+F2iYPQu0g==);
maven 坐标如下:
-
<dependency>
-
<groupId>com.github.ulisesbocchio</groupId>
-
<artifactId>jasypt-spring-boot-starter</artifactId>
-
<version>2.0.0</version>
-
</dependency>
相比第一種用法,maven 坐标有所變化。但是不需要顯示增加注解 @EnableEncryptableProperties;
github位址
github:https://github.com/ulisesbocchio/jasypt-spring-boot 它 github 首頁有詳細的用法說明,以及一些自定義特性,例如使用自定義的字首和字尾取代 ENC():
-
jasypt.encryptor.property.prefix=ENC@[
-
jasypt.encryptor.property.suffix=]
原了解密
既然是 springboot 方式內建,那麼首先看 jasypt-spring-boot 的 spring.factories 的申明:
-
org.springframework.context.ApplicationListener=\
-
com.ulisesbocchio.jasyptspringboot.configuration.EnableEncryptablePropertiesBeanFactoryPostProcessor
這個類的部分核心源碼如下:
-
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。
原文釋出時間為:2018-09-14
本文作者:
Java技術驿站本文來自雲栖社群合作夥伴“
”,了解相關資訊可以關注“
”。