Get史上最優雅的加密方式!沒有之一

你的配置文件是不是還在使用下面這種落後的配置暴露一些密碼:

jdbc.url=jdbc:mysql://127.0.0.1:3305/afei
jdbc.username=afei
jdbc.password=123456

如果是,那麼繼續往下看。筆者今天介紹史上最優雅加密接入方式:jasypt

使用方式

  • 用法一

先看用法有多簡單,以springboot為例:

  1. Application.java上增加註解@EnableEncryptableProperties;
  2. 增加配置文件jasypt.encryptor.password = Afei@2018,這是加密的秘鑰;
  3. 所有明文密碼替換為ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
  4. 引入一個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指定的值,即秘鑰。

  • 用法二

其實還有另一種更簡單的姿勢:

  1. 增加配置文件jasypt.encryptor.password = Afei@2018,這是加密的秘鑰;
  2. 所有明文密碼替換為ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);
  3. 引入一個MAVEN依賴;

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;

}
}
/<applicationevent>

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。


分享到:


相關文章: