SpringBoot整合jasypt實現(xiàn)敏感信息的加密詳解
一、簡介
在后端開發(fā)中有很多敏感信息,比如數(shù)據(jù)庫用戶名密碼,第三方 Apikey,云服務(wù)商的 secretKey 等、如果不希望用明文在 application.yml 配置的,可以使用 jasypt 加密這些字段。
還有很重要的一點,如果你自己開源一些東西,將代碼上傳一些代碼托管平臺,肯定需要隱藏敏感信息,用 jasypt 加密可以簡化每次上傳下拉代碼修改敏感信息。
官方文檔, 官方文檔使用方法描述得很清楚適用于各種情況,下面我簡單記錄一下加密 MySQL 用戶名密碼方法
二、導(dǎo)入依賴
<dependencies>
<!-- jasypt 敏感數(shù)據(jù)加密,如:數(shù)據(jù)庫密碼,阿里云短信服務(wù)等-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- springboot 啟動包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
三、加密字段工具類
加密 mysql 用戶名密碼,將用戶名密碼傳入 fields 數(shù)組,保存打印結(jié)果下面配置在 application.yaml 文件
public class JasyptUtil {
private static PooledPBEStringEncryptor encryptor;
static{
encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("This is a secret key"); // 秘鑰
config.setAlgorithm("PBEWithMD5AndDES");
//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);
}
public static void main(String[] args) {
// 需要加密的字段
String[] fields = {"root","123456"};
for (String field : fields) {
System.out.println(field+"---->"+encryptorField(field));
}
}
public static String encryptorField(String field){
return encryptor.encrypt(field);
}
public static String decryptField(String field){
return encryptor.decrypt(field);
}
}可以看到加密過后的字符串如下

四、application.yaml 配置
數(shù)據(jù)源用戶名密碼使用上面生成加密字段
spring:
datasource:
username: ENC(J5GOvO1FBgtiwEytIjU/4WdzHUgbJq/W)
password: ENC(SqCHgntWcYnthvtWGA3+GAycDle/qCBx)
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/oauth?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8
# jasypt 敏感數(shù)據(jù)加密配置
# 詳細用法可參考 https://github.com/ulisesbocchio/jasypt-spring-boot
jasypt:
encryptor:
password: 123456 # 秘鑰,除了該項,下面都是默認值,該項建議設(shè)置 JVM 啟動參數(shù),如:-Djasypt.encryptor.password=123456
algorithm: PBEWithMD5AndDES # 加密算法
key-obtention-iterations: 1000 # 迭代次數(shù),值越大越復(fù)雜,相對越安全
pool-size: 1
provider-name: SunJCE
salt-generator-classname: org.jasypt.salt.RandomSaltGenerator
iv-generator-classname: org.jasypt.iv.RandomIvGenerator
string-output-type: base64
proxy-property-sources: false
property:
prefix: ENC( # 默認前綴
suffix: ) # 默認后綴
五、啟動類測試
查詢 MySQL 的 user 表打印用戶名和密碼
package com.ye;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@SpringBootApplication
public class Test2Application {
public static void main(String[] args) throws SQLException {
ConfigurableApplicationContext context = SpringApplication.run(Test2Application.class, args);
DataSource dataSource = (DataSource) context.getBean("dataSource");
Connection connection = dataSource.getConnection();
try {
PreparedStatement ps = connection.prepareStatement("select * from user;");
ResultSet rs = ps.executeQuery();
System.out.println("<---------- user 表數(shù)據(jù) ----------->");
while (rs.next()) {
String userName = rs.getString("user_name");
String password = rs.getString("password");
System.out.printf("userName: %s, password: %s%n", userName, password);
}
} catch (SQLException ex) {
ex.printStackTrace();
connection.close();
}
}
}點擊 Edit Configurations ,添加VM 啟動參數(shù) -Djasypt.encryptor.password="This is a secret key",這個密碼和加密工具類 JasyptUtil 密碼保持一致

運行成功打印結(jié)果如下

經(jīng)過上面的操作,我們已經(jīng)將敏感信息加密
到此這篇關(guān)于SpringBoot整合jasypt實現(xiàn)敏感信息的加密詳解的文章就介紹到這了,更多相關(guān)SpringBoot敏感信息加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring的BeanFactoryPostProcessor接口示例代碼詳解
這篇文章主要介紹了Spring的BeanFactoryPostProcessor接口,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

