天天看點

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

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

  1. jdbc.url=jdbc:mysql://127.0.0.1:3305/afei

  2. jdbc.username=afei

  3. jdbc.password=123456

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

使用方式

用法一

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

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

maven坐标如下:

  1. <dependency>

  2. <groupId>com.github.ulisesbocchio</groupId>

  3. <artifactId>jasypt-spring-boot</artifactId>

  4. <version>2.0.0</version>

  5. </dependency>

簡答的 4 步就搞定啦,是不是超簡單?完全不需要修改任何業務代碼。其中第三步的加密字元串的生成方式為:

  1. 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==);

maven 坐标如下:

  1. <dependency>

  2. <groupId>com.github.ulisesbocchio</groupId>

  3. <artifactId>jasypt-spring-boot-starter</artifactId>

  4. <version>2.0.0</version>

  5. </dependency>

相比第一種用法,maven 坐标有所變化。但是不需要顯示增加注解 @EnableEncryptableProperties;

github位址

github:https://github.com/ulisesbocchio/jasypt-spring-boot 它 github 首頁有詳細的用法說明,以及一些自定義特性,例如使用自定義的字首和字尾取代 ENC():

  1. jasypt.encryptor.property.prefix=ENC@[

  2. jasypt.encryptor.property.suffix=]

原了解密

既然是 springboot 方式內建,那麼首先看 jasypt-spring-boot 的 spring.factories 的申明:

  1. org.springframework.context.ApplicationListener=\

  2. com.ulisesbocchio.jasyptspringboot.configuration.EnableEncryptablePropertiesBeanFactoryPostProcessor

這個類的部分核心源碼如下:

  1. public class EnableEncryptablePropertiesBeanFactoryPostProcessor implements BeanFactoryPostProcessor, ApplicationListener<ApplicationEvent>, Ordered {

  2. @Override

  3. public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

  4. // 得到加密字元串的處理類(已經加密的密碼通過它來解密)

  5. EncryptablePropertyResolver propertyResolver = beanFactory.getBean(RESOLVER_BEAN_NAME, EncryptablePropertyResolver.class);

  6. // springboot下的Environment裡包含了所有我們定義的屬性, 也就包含了application.properties中所有的屬性

  7. MutablePropertySources propSources = environment.getPropertySources();

  8. // 核心,PropertySource的getProperty(String)方法委托給EncryptablePropertySourceWrapper

  9. convertPropertySources(interceptionMode, propertyResolver, propSources);

  10. }

  11. @Override

  12. public int getOrder() {

  13. // 讓這個jasypt定義的BeanFactoryPostProcessor的初始化順序最低,即最後初始化

  14. return Ordered.LOWEST_PRECEDENCE;

  15. }

  16. }

PropertySource 的

getProperty(String)

方法委托給EncryptablePropertySourceWrapper,那麼當擷取屬性時,實際上就是調用 EncryptablePropertySourceWrapper 的

getProperty()

方法,在這個方法裡我們就能對 value 進行解密了。

EncryptablePropertySourceWrapper 實作了接口EncryptablePropertyResolver,該定義如下:

  1. // An interface to resolve property values that may be encrypted.

  2. public interface EncryptablePropertyResolver {

  3. String resolvePropertyValue(String value);

  4. }

接口描述: 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 包裹的屬性,那麼傳回解密後的值;
  • 如果沒有被包裹,那麼傳回原生的值;

實作類的實作如下:

  1. @Override

  2. public String resolvePropertyValue(String value) {

  3. String actualValue = value;

  4. // 如果value是加密的value,則進行解密。

  5. if (detector.isEncrypted(value)) {

  6. try {

  7. // 解密算法核心實作

  8. actualValue = encryptor.decrypt(detector.unwrapEncryptedValue(value.trim()));

  9. } catch (EncryptionOperationNotPossibleException e) {

  10. // 如果解密失敗,那麼抛出異常。

  11. throw new DecryptionException("Decryption of Properties failed, make sure encryption/decryption passwords match", e);

  12. }

  13. }

  14. // 沒有加密的value,傳回原生value即可

  15. return actualValue;

  16. }

判斷是否是加密的邏輯很簡單:

(trimmedValue.startsWith(prefix)&&trimmedValue.endsWith(suffix))

,即隻要 value 是以 prefixe/suffixe 包括,就認為是加密的 value。

總結

通過對源碼的分析可知 jasypt 的原理很簡單,就是講原本 spring 中PropertySource 的 getProperty(String) 方法委托給我們自定義的實作。然後再自定義實作中,判斷 value 是否是已經加密的 value ,如果是,則進行解密。如果不是,則傳回原 value。

原文釋出時間為:2018-09-14

本文作者:

Java技術驿站

本文來自雲栖社群合作夥伴“

”,了解相關資訊可以關注“

”。