SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解
一、簡(jiǎn)介
在后端開發(fā)中有很多敏感信息,比如數(shù)據(jù)庫(kù)用戶名密碼,第三方 Apikey,云服務(wù)商的 secretKey 等、如果不希望用明文在 application.yml 配置的,可以使用 jasypt 加密這些字段。
還有很重要的一點(diǎn),如果你自己開源一些東西,將代碼上傳一些代碼托管平臺(tái),肯定需要隱藏敏感信息,用 jasypt 加密可以簡(jiǎn)化每次上傳下拉代碼修改敏感信息。
官方文檔, 官方文檔使用方法描述得很清楚適用于各種情況,下面我簡(jiǎn)單記錄一下加密 MySQL 用戶名密碼方法
二、導(dǎo)入依賴
<dependencies>
<!-- jasypt 敏感數(shù)據(jù)加密,如:數(shù)據(jù)庫(kù)密碼,阿里云短信服務(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 啟動(dòng)包 -->
<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);
}
}可以看到加密過(guò)后的字符串如下

四、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ù)加密配置
# 詳細(xì)用法可參考 https://github.com/ulisesbocchio/jasypt-spring-boot
jasypt:
encryptor:
password: 123456 # 秘鑰,除了該項(xiàng),下面都是默認(rèn)值,該項(xiàng)建議設(shè)置 JVM 啟動(dòng)參數(shù),如:-Djasypt.encryptor.password=123456
algorithm: PBEWithMD5AndDES # 加密算法
key-obtention-iterations: 1000 # 迭代次數(shù),值越大越復(fù)雜,相對(duì)越安全
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( # 默認(rèn)前綴
suffix: ) # 默認(rèn)后綴
五、啟動(dòng)類測(cè)試
查詢 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();
}
}
}點(diǎn)擊 Edit Configurations ,添加VM 啟動(dòng)參數(shù) -Djasypt.encryptor.password="This is a secret key",這個(gè)密碼和加密工具類 JasyptUtil 密碼保持一致

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

經(jīng)過(guò)上面的操作,我們已經(jīng)將敏感信息加密
到此這篇關(guān)于SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解的文章就介紹到這了,更多相關(guān)SpringBoot敏感信息加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 中Collection存儲(chǔ)器詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了java 中Collection存儲(chǔ)器詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04
Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的長(zhǎng)輪詢的示例代碼
長(zhǎng)輪詢是與服務(wù)器保持即時(shí)通信的最簡(jiǎn)單的方式,它不使用任何特定的協(xié)議,例如 WebSocket ,所以也不依賴于瀏覽器版本等外部條件的兼容性。本文將用Java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的長(zhǎng)輪詢,需要的可以參考一下2022-08-08
Spring的BeanFactoryPostProcessor接口示例代碼詳解
這篇文章主要介紹了Spring的BeanFactoryPostProcessor接口,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
解決Spring導(dǎo)出可以運(yùn)行的jar包問(wèn)題
最近需要解決Maven項(xiàng)目導(dǎo)入可執(zhí)行的jar包的問(wèn)題,如果項(xiàng)目不包含Spring,那么使用mvn assembly:assembly即可,這篇文章主要介紹了Spring導(dǎo)出可以運(yùn)行的jar包,需要的朋友可以參考下2023-03-03

