【Springboot】Springboot整合Jasypt,讓配置信息安全最優雅方便的方式

1 簡介

在上一篇文章中,介紹了Jasypt及其用法,具體細節能夠查看【Java庫】如何使用優秀的加密庫Jasypt來保護你的敏感信息?。如此利器,用之得當,那將事半功倍。本文將介紹Springboot整合Jasypt,實現配置信息的安全,如數據庫鏈接、帳號和密碼、接口憑證信息等。html

Jasypt能夠爲Springboot加密的信息不少,主要有:java

  • System Property 系統變量
  • Envirnment Property 環境變量
  • Command Line argument 命令行參數
  • Application.properties 應用配置文件
  • Yaml properties 應用配置文件
  • other custom property sources 其它配置文件

經測試,Springboot 2.1.9版本與jasypt-spring-boot最新版本的3.0.0和2.1.2均有問題,本文使用2.1.1成功。linux

2 如何加入依賴

Jasypt整合到Springboot是另外一個開源項目jasypt-spring-boot,主要有三種整合方式:git

2.1 jasypt-spring-boot-starter

若是項目使用@SpringBootApplication@EnableAutoConfiguration註解,在pom中加入如下依賴便可對整個Spring的環境的配置信息進行加密解密。github

<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot-starter</artifactId>
  <version>2.1.1</version>
</dependency>複製代碼

2.2 jasypt-spring-boot

若是項目不使用@SpringBootApplication@EnableAutoConfiguration註解,咱們就使用下面的依賴,而後在配置Java類中加上註解@EnableEncryptablePropertiesspring

<dependency>
  <groupId>com.github.ulisesbocchio</groupId>
  <artifactId>jasypt-spring-boot</artifactId>
  <version>2.1.1</version>
</dependency>複製代碼

配置類以下:數據庫

@Configuration
@EnableEncryptableProperties
public class MyApplication {
 
}複製代碼

2.3 只對特定配置加密解密

若是不想使用以上兩種方式對全部配置信息都進行加密解密的話,可使用註解@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 {
    
}複製代碼

3 生成加密字符

生成加密字符有多種方式,在實踐中使用過如下幾種方式。服務器

3.1 Java命令行

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==複製代碼

3.2 腳本命令

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==複製代碼

3.3 Java代碼

既然是Java的庫,那確定能用Java代碼來加密解密了。具體細節能夠參考【Java庫】如何使用優秀的加密庫Jasypt來保護你的敏感信息?

4 配置密文與其它項

4.1 配置密文

生成密文後,就要把密文配置在相應的位置,以下:

username: ENC(SUfiOs8MvmAUjg+oWl/6dQ==)

jasypt:
  encryptor:
    password: pkslow
    algorithm: PBEWithMD5AndTripleDES複製代碼

配置密文的默認格式:ENC(密文),這個格式能夠經過jasypt.encryptor.property.prefixjasypt.encryptor.property.suffix配置,這裏再也不演示。

4.2 其它配置項

配置信息只有 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

5 如何安放你的密鑰

密鑰是很是重要的信息,放在什麼地方,決定着你的密文是否真的安全。能夠有如下幾類方式:

(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包並反編譯的人,才能得到密文。

若是咱們把密鑰的一部分寫在代碼裏,另外一部分經過外部方式來配置,這樣就會更加安全。

6 結果測試

咱們已經完成了密文的生成,如今咱們測試一下是否能正常解密,測試代碼以下:

@RestController
@RequestMapping("/jasypt")
public class JasyptController {
    @Value("${username}")
    private String username;

    @GetMapping("/name")
    public Mono<String> sendNormalText() {
        return Mono.just(username);
    }
}複製代碼

訪問該接口,能返回加密前的字符串,整個流程測試成功:

file

7 總結

本文簡介了Springboot整合Jasypt實現配置信息的安全化,在實際項目中應用仍是不少的。

另外,若是項目中是採用Spring Cloud Config的,它提供了統一的加解密方式,也方便使用。但若是應用配置沒有走配置中心,仍是應該使用Jasypt。

歡迎關注公衆號<南瓜慢說>,將持續爲你更新...

file

歡迎加博主微信,作一個點贊之友,哈哈...

file

多讀書,多分享;多寫做,多整理。

相關文章
相關標籤/搜索