在上一篇文章中,介紹了Jasypt
及其用法,具體細節能夠查看【Java庫】如何使用優秀的加密庫Jasypt來保護你的敏感信息?。如此利器,用之得當,那將事半功倍。本文將介紹Springboot整合Jasypt
,實現配置信息的安全,如數據庫鏈接、帳號和密碼、接口憑證信息等。html
Jasypt能夠爲Springboot加密的信息不少,主要有:java
經測試,Springboot 2.1.9版本與jasypt-spring-boot最新版本的3.0.0和2.1.2均有問題,本文使用2.1.1成功。linux
Jasypt整合到Springboot是另外一個開源項目jasypt-spring-boot,主要有三種整合方式:git
若是項目使用@SpringBootApplication
或@EnableAutoConfiguration
註解,在pom中加入如下依賴便可對整個Spring的環境的配置信息進行加密解密。github
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>
若是項目不使用@SpringBootApplication
或@EnableAutoConfiguration
註解,咱們就使用下面的依賴,而後在配置Java類中加上註解@EnableEncryptableProperties
。spring
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.1</version> </dependency>
配置類以下:數據庫
@Configuration @EnableEncryptableProperties public class MyApplication { }
若是不想使用以上兩種方式對全部配置信息都進行加密解密的話,可使用註解@EncryptablePropertySource
指定配置文件,依賴以下:api
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>2.1.1</version> </dependency>
配置類以下:安全
@Configuration @EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties") public class MyApplication { }
生成加密字符有多種方式,在實踐中使用過如下幾種方式。bash
Jasypt提供了一個類專門用於加密解密,提供了main方法,調用以下:
java -cp ./jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI password=pkslow algorithm=PBEWithMD5AndTripleDES input=larry
輸出爲:
----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.212-b10 ----ARGUMENTS------------------- input: larry algorithm: PBEWithMD5AndTripleDES password: pkslow ----OUTPUT---------------------- SUfiOs8MvmAUjg+oWl/6dQ==
Jasypt爲咱們提供了腳本,能夠直接用於加密解密,從http://www.jasypt.org/download.html能夠下載。下載解壓後的文件有:
# 解壓後文件 LICENSE.txt NOTICE.txt README.txt apidocs bin lib # bin文件夾的文件 decrypt.bat decrypt.sh digest.bat digest.sh encrypt.bat encrypt.sh listAlgorithms.bat listAlgorithms.sh
在bin目錄下面,咱們能夠根據本身的系統選擇使用什麼腳原本生成密文,使用參數與Java命令同樣。其實這些腳本就是封裝了一個調用Java類的工具。使用以下:
$ sh encrypt.sh password=pkslow algorithm=PBEWithMD5AndTripleDES input=larry ----ENVIRONMENT----------------- Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.212-b10 ----ARGUMENTS------------------- input: larry algorithm: PBEWithMD5AndTripleDES password: pkslow ----OUTPUT---------------------- xRvdeEnk7zgKtX5uVGCIug==
既然是Java的庫,那確定能用Java代碼來加密解密了。具體細節能夠參考【Java庫】如何使用優秀的加密庫Jasypt來保護你的敏感信息?。
生成密文後,就要把密文配置在相應的位置,以下:
username: ENC(SUfiOs8MvmAUjg+oWl/6dQ==) jasypt: encryptor: password: pkslow algorithm: PBEWithMD5AndTripleDES
配置密文的默認格式:ENC(密文),這個格式能夠經過jasypt.encryptor.property.prefix
和jasypt.encryptor.property.suffix
配置,這裏再也不演示。
配置信息只有 jasypt.encryptor.password 是必須的,配置項有:
配置項 | 必須 | Default Value |
---|---|---|
jasypt.encryptor.password | True | - |
jasypt.encryptor.algorithm | False | PBEWITHHMACSHA512ANDAES_256 |
jasypt.encryptor.keyObtentionIterations | False | 1000 |
jasypt.encryptor.poolSize | False | 1 |
jasypt.encryptor.providerName | False | SunJCE |
jasypt.encryptor.providerClassName | False | null |
jasypt.encryptor.saltGeneratorClassname | False | org.jasypt.salt.RandomSaltGenerator |
jasypt.encryptor.ivGeneratorClassname | False | org.jasypt.iv.RandomIvGenerator |
jasypt.encryptor.stringOutputType | False | base64 |
jasypt.encryptor.proxyPropertySources | False | false |
密鑰是很是重要的信息,放在什麼地方,決定着你的密文是否真的安全。能夠有如下幾類方式:
(1)放在application.properties
這樣能得到配置文件的人就能知道密鑰,不夠安全。但它是一種方便簡單的方式。存在密文和密鑰放在同一個配置文件的風險。
(2)JVM參數
在啓動Java程序時加參數:-Djasypt.encryptor.password=pkslow
,這樣就不會把密鑰放在代碼中去了。
(3)服務器的環境變量
把密鑰放在linux系統的環境變量中去,只有能拿到服務器訪問權限的人,纔有可能知道密鑰在哪。例如:
# 配置profile文件 export JASYPT_PASSWORD = pkslow # 生效 source /etc/profile # 運行java程序時 java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
(4)使用自定義的Encryptor來存放
以上咱們都使用了官方提供的Encryptor,其實咱們是能夠自定義的,以下:
@Bean("jasyptStringEncryptor") public StringEncryptor stringEncryptor() { PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword("password"); config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName("SunJCE"); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator"); config.setStringOutputType("base64"); encryptor.setConfig(config); return encryptor; }
把密鑰寫在代碼裏,只有能得到jar包並反編譯的人,才能得到密文。
若是咱們把密鑰的一部分寫在代碼裏,另外一部分經過外部方式來配置,這樣就會更加安全。
咱們已經完成了密文的生成,如今咱們測試一下是否能正常解密,測試代碼以下:
@RestController @RequestMapping("/jasypt") public class JasyptController { @Value("${username}") private String username; @GetMapping("/name") public Mono<String> sendNormalText() { return Mono.just(username); } }
訪問該接口,能返回加密前的字符串,整個流程測試成功:
本文簡介了Springboot整合Jasypt實現配置信息的安全化,在實際項目中應用仍是不少的。
另外,若是項目中是採用Spring Cloud Config的,它提供了統一的加解密方式,也方便使用。但若是應用配置沒有走配置中心,仍是應該使用Jasypt。
歡迎關注公衆號<南瓜慢說>,將持續爲你更新...
歡迎加博主微信,作一個點贊之友,哈哈...
多讀書,多分享;多寫做,多整理。