SpringBoot 集成 Jasypt 對數(shù)據(jù)庫加密以及踩坑的記錄分享
前言
密碼安全是非常重要的,因此我們在代碼中往往需要對密碼進行加密,以此保證密碼的安全
加依賴
<!-- jasypt --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency>
加配置
# jasypt 密碼加密配置 jasypt: encryptor: # 加密鹽值 password: jasypt # 加密算法設(shè)置 3.0.0 以后 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator
PS:可以看到配置中特意配置了加密算法,原因是官方在 3.0.0 以后更改了加密算法,所以假如你不設(shè)置的話,使用網(wǎng)上的方法加密出來的密碼啟動就會報錯,如圖:

官方 issue:Failed to bind properties under ‘spring.datasource.password' to java.lang.String` #154

版本在 3.0.0 之前的 Jasypt
需要額外添加 Jasypt 的加密鹽值配置到 Tomcat
-Djasypt.encryptor.password=xxxx
工具類
如果你想手動使用 Jasypt 進行加解密
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class JasyptUtil {
/**
* Jasypt生成加密結(jié)果
* @param password 配置文件中設(shè)定的加密鹽值
* @param value 加密值
* @return
*/
public static String encyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
String result = encryptor.encrypt(value);
return result;
}
/**
* 解密
* @param password 配置文件中設(shè)定的加密鹽值
* @param value 解密密文
* @return
*/
public static String decyptPwd(String password,String value){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
encryptor.setConfig(cryptor(password));
String result = encryptor.decrypt(value);
return result;
}
public static SimpleStringPBEConfig cryptor(String password){
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(password);
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args) {
// 加密
String encPwd = encyptPwd("jasypt", "123456");
// 解密
String decPwd = decyptPwd("jasypt", encPwd);
System.out.println(encPwd);
System.out.println(decPwd);
}
}
數(shù)據(jù)庫配置解密
用官方提供的保留字 ENC,將加密的密碼包裹即可
spring: datasource: url: jdbc:mysql://xx.xx.xx.xx/xxxx?useUnicode=true&characterEncoding=utf8&useSSL=false username: root password: ENC(加密后的密碼) driver-class-name: com.mysql.jdbc.Driver
到此這篇關(guān)于SpringBoot 集成 Jasypt 對數(shù)據(jù)庫加密以及踩坑的記錄分享的文章就介紹到這了,更多相關(guān)SpringBoot 集成 Jasypt 對數(shù)據(jù)庫加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis中實體類屬性與數(shù)據(jù)列表間映射方法介紹
這篇文章主要介紹了Mybatis中實體類屬性與數(shù)據(jù)列表間映射方法介紹,一共四中方法,供大家參考。2017-10-10
Java如何優(yōu)雅地避免空指針異常(NullPointerException)
這篇文章主要給大家介紹了關(guān)于Java如何優(yōu)雅地避免空指針異常(NullPointerException)的相關(guān)資料,空指針異常(NullPointerException)是一種常見的運行時異常,它在Java編程中經(jīng)常出現(xiàn),需要的朋友可以參考下2024-03-03
JavaEE開發(fā)基于Eclipse的環(huán)境搭建以及Maven Web App的創(chuàng)建
本文主要介紹了如何在Eclipse中創(chuàng)建的Maven Project,本文是JavaEE開發(fā)的開篇,也是基礎(chǔ)。下面內(nèi)容主要包括了JDK1.8的安裝、JavaEE版本的Eclipse的安裝、Maven的安裝、Tomcat 9.0的配置、Eclipse上的M2Eclipse插件以及STS插件的安裝。2017-03-03
Java中@RequiredArgsConstructor注解的基本用法
這篇文章主要介紹了Java中@RequiredArgsConstructor注解的基本用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-09-09
Java AQS中ReentrantReadWriteLock讀寫鎖的使用
ReentrantReadWriteLock稱為讀寫鎖,它提供一個讀鎖,支持多個線程共享同一把鎖。這篇文章主要講解一下ReentrantReadWriteLock的使用和應(yīng)用場景,感興趣的可以了解一下2023-02-02

