使用方式
你的配置文件是不是還在使用下面這種落后的配置暴露一些密碼:
jdbc.url=jdbc:mysql://127.0.0.1:3305/afeidb
jdbc.username=afei
jdbc.password=123456
如果是隅俘,那么繼續(xù)往下看为居。筆者今天介紹史上最優(yōu)雅加密接入方式:jasypt。
- 用法一
先看用法有多簡單贰镣,以springboot為例:
- Application.java上增加注解@EnableEncryptableProperties;
- 增加配置文件jasypt.encryptor.password = Afei@2018恭陡,這是加密的秘鑰上煤;
- 所有明文密碼替換為ENC(加密字符串)劫狠,例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
- 引入一個MAVEN依賴呐矾;
maven坐標(biāo)如下:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
簡答的4步就搞定啦凫佛,是不是超簡單愧薛?完全不需要修改任何業(yè)務(wù)代碼衫画。 其中第三步的加密字符串的生成方式為:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=Afei@2018 algorithm=PBEWithMD5AndDES
其中:
- input的值就是原密碼。
- password的值就是參數(shù)jasypt.encryptor.password指定的值瞄勾,即秘鑰弥激。
- 用法二
其實還有另一種更簡單的姿勢:
- 增加配置文件jasypt.encryptor.password = Afei@2018微服,這是加密的秘鑰以蕴;
- 所有明文密碼替換為ENC(加密字符串)糙麦,例如ENC(XW2daxuaTftQ+F2iYPQu0g==)赡磅;
- 引入一個MAVEN依賴焚廊;
maven坐標(biāo)如下:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
相比第一種用法,maven坐標(biāo)有所變化徙硅。但是不需要顯示增加注解@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 {
// 得到加密字符串的處理類(已經(jīng)加密的密碼通過它來解密)
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笼吟,那么當(dāng)獲取屬性時贷帮,實際上就是調(diào)用EncryptablePropertySourceWrapper的getProperty()方法诱告,在這個方法里我們就能對value進行解密了蔬啡。
EncryptablePropertySourceWrapper實現(xiàn)了接口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包裹的屬性,那么返回解密后的值箱蟆;
- 如果沒有被包裹沟绪,那么返回原生的值;
實現(xiàn)類的實現(xiàn)如下:
@Override
public String resolvePropertyValue(String value) {
String actualValue = value;
// 如果value是加密的value空猜,則進行解密绽慈。
if (detector.isEncrypted(value)) {
try {
// 解密算法核心實現(xiàn)
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包括前酿,就認(rèn)為是加密的value培漏。
總結(jié)
通過對源碼的分析可知jasypt的原理很簡單晕鹊,就是講原本spring中PropertySource的getProperty(String)方法委托給我們自定義的實現(xiàn)盏触。然后再自定義實現(xiàn)中吓肋,判斷value是否是已經(jīng)加密的value哟沫,如果是猾警,則進行解密。如果不是雳窟,則返回原value捣作。