Springboot實(shí)現(xiàn)ENC加密的詳細(xì)流程
1. 為什么要用ENC加密
以下是未經(jīng)過加密的數(shù)據(jù)庫配置,密碼均是采用明文密碼,很容易導(dǎo)致數(shù)據(jù)庫泄露。
spring: datasource: dynamic: postgresql: url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai username: root password: admin driver-class-name: org.postgresql.Driver ... redis: ip: www.xxxx.top port: 6379 # 密碼 pass: admin # 最大實(shí)例 max-total: 1024 # 最大空閑實(shí)例 max-idle: 100 # 等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)。 max-wait: 10000 # 超時(shí)時(shí)間,單位毫秒。 timeout: 10000
以下是經(jīng)過ENC加密之后的配置,這樣之后,數(shù)據(jù)庫密碼安全級別就高了。
spring: datasource: dynamic: postgresql: url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai username: root password: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q) driver-class-name: org.postgresql.Driver ... redis: ip: www.xxxx.top port: 6379 # 密碼 pass: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q) # 最大實(shí)例 max-total: 1024 # 最大空閑實(shí)例 max-idle: 100 # 等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)。 max-wait: 10000 # 超時(shí)時(shí)間,單位毫秒。 timeout: 10000
2. jasypt實(shí)現(xiàn)ENC加密
1. 實(shí)現(xiàn)流程
導(dǎo)入依賴:
<!-- 配置文件加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>測試類:
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @Author: chenJY
* @Description:
* @Date: 2022-11-18 9:19
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class EncryptorTest {
@Resource
private StringEncryptor jasyptStringEncryptor;
@Test
public void encode() {
System.out.println( "加密密文:" + jasyptStringEncryptor.encrypt("admin") );
System.out.println("解密密文:" + jasyptStringEncryptor.decrypt(jasyptStringEncryptor.encrypt("admin")));
}
}
運(yùn)行測試類:

注意: 每次運(yùn)行測試類輸出的加密密碼都不同,但不影響其解密密文。
將加密密文加入到配置文件中:
spring: datasource: dynamic: postgresql: url: jdbc:postgresql://127.0.0.1:5589/mypg?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai username: root password: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q) driver-class-name: org.postgresql.Driver ... redis: ip: www.xxxx.top port: 6379 # 密碼 pass: ENC(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q) # 最大實(shí)例 max-total: 1024 # 最大空閑實(shí)例 max-idle: 100 # 等待可用連接的最大時(shí)間,單位毫秒,默認(rèn)值為-1,表示永不超時(shí)。 max-wait: 10000 # 超時(shí)時(shí)間,單位毫秒。 timeout: 10000
重啟springboot項(xiàng)目,能正常啟動
2. 說明
由于springboot自動配置的特性,導(dǎo)入 jasypt-spring-boot 依賴包之后,不用進(jìn)行過多配置,就能實(shí)現(xiàn)配置文件加密字段自動解密,所以特別方便。
1. 自定義加密秘鑰
1. 鹽、前綴、后綴
可以在配置文件中自定義一個(gè)加密秘鑰(鹽), 來獲取明文密碼。
# 加密秘鑰
jasypt:
encryptor:
password: Chen # 加密時(shí)的salt值
自定義加密前綴、后綴: 如果不想使用 ENC來作為加密前綴,那么可以通過配置文件修改:
# 加密秘鑰
jasypt:
encryptor:
password: Chen
property:
prefix: Chen( # 前綴
suffix: )chen # 后綴那么,密碼的格式如下:
password: Chen(2qWZr4WLw8H0nPBzMXBV9WimRfKC3pv5UVGB5mMApbINg2s83VfsYvL7XTWaUR8q)chen
2. 自定義加密方案
配置類
@Configuration
public class MyEncryptorCfg {
/**
* @Description 自定義的加密器配置
* @author chenJY
* @date 2022/11/18 9:52
* @return StringEncryptor
*/
@Bean(name = "myStringEncryptor")
public StringEncryptor myStringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("Chen");
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;
}
}注意1: bean必須重命名,bean默認(rèn)名是 jasyptStringEncryptor,當(dāng)我們要自定義加密方案的時(shí)候,就必須重命名。
注意2: 需要在配置文件中加入如下配置:
jasypt:
encryptor:
bean: myStringEncryptor并修改測試類:
@Autowired
private StringEncryptor myStringEncryptor;2. 部署方案
密鑰(鹽值)存儲說明: 本身加解密過程都是通過鹽值進(jìn)行處理的,所以正常情況下鹽值和加密串是分開存儲的。鹽值應(yīng)該放在系統(tǒng)屬性、命令行或是環(huán)境變量來使用,而不是放在配置文件。
程序啟動 命令行參數(shù):
java -jar xxx.jar --jasypt.encryptor.password=Chen &
程序啟動 環(huán)境變量:
java -jar -Djasypt.encryptor.password=Chen xxx.jar
3. 輸出密文的幾種方案
優(yōu)化1.: 上面的寫法是直接寫死了需要加密的密碼,我們可以換一種在配置文件中讀取數(shù)據(jù)庫密碼的寫法,如下:
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* @Author: chenJY
* @Description:
* @Date: 2022-11-18 9:19
*/
@SpringBootTest
@RunWith(SpringRunner.class)
public class EncryptorTest {
@Resource
private ApplicationContext applicationContext;
@Resource
private StringEncryptor jasyptStringEncryptor;
@Test
public void encode() {
Environment environment = applicationContext.getEnvironment();
String password = environment.getProperty("spring.datasource.dynamic.postgresql.password");
System.out.println( "加密密文:" + jasyptStringEncryptor.encrypt(password) );
System.out.println("解密密文:" + jasyptStringEncryptor.decrypt(jasyptStringEncryptor.encrypt(password)));
}
}
優(yōu)化2: 重寫啟動類的run(),實(shí)現(xiàn)每次啟動項(xiàng)目都會輸出一次加密密文
import org.jasypt.encryption.StringEncryptor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
@SpringBootApplication
public class TestDemoApplication implements CommandLineRunner{
@Resource
private ApplicationContext applicationContext;
@Resource
private StringEncryptor jasyptStringEncryptor;
public static void main(String[] args) {
SpringApplication.run(TestDemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Environment environment = applicationContext.getEnvironment();
String pwd = environment.getProperty("spring.datasource.dynamic.postgresql.password");
// 打印解密后的結(jié)果
System.out.println( "加密密文:" + jasyptStringEncryptor.encrypt(pwd) );
}
}
總結(jié)
到此這篇關(guān)于Springboot實(shí)現(xiàn)ENC加密的詳細(xì)流程的文章就介紹到這了,更多相關(guān)Springboot實(shí)現(xiàn)ENC加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)隊(duì)列的三種方法集合
這篇文章主要介紹了Java實(shí)現(xiàn)隊(duì)列的三種方法集合,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
如何利用JAVA正則表達(dá)式輕松替換JSON中的大字段
這篇文章主要給大家介紹了關(guān)于如何利用JAVA正則表達(dá)式輕松替換JSON中大字段的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
java實(shí)現(xiàn)員工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)員工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Java?設(shè)計(jì)模式以虹貓藍(lán)兔的故事講解原型模式
原型模式是用于創(chuàng)建重復(fù)的對象,同時(shí)又能保證性能。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的最佳方式,今天通過本文給大家介紹下Java 原型設(shè)計(jì)模式,感興趣的朋友一起看看吧2022-04-04
spring data jpa開啟批量插入、批量更新的問題解析
這篇文章主要介紹了spring data jpa開啟批量插入、批量更新問題,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
mybatis連接mysql的實(shí)現(xiàn)過程
通過配置Maven的pom文件,可以簡化MyBatis連接數(shù)據(jù)庫的過程,免去手動下載和導(dǎo)入各種依賴包的麻煩,本文介紹了如何利用Maven導(dǎo)入MyBatis及其他相關(guān)依賴,如Junit、MySQL連接驅(qū)動、Druid連接池和Dbutil等,以簡化數(shù)據(jù)庫操作和測試2024-10-10

