SpringBoot保護(hù)配置文件中敏感信息的保姆級教程
前言
公司部門檢查,要求系統(tǒng)配置文件中的敏感信息如數(shù)據(jù)庫密碼等,進(jìn)行加密處理,否則將受到公司的安全處罰,無奈只要按照公司要求,對springboot項(xiàng)目配置文件的敏感信息進(jìn)行加密和解密處理。詳細(xì)教程如下。
第一步:在pom.xml添加第三方依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
第二步:添加加密配置
方式一:通過配置問年間 application.yml中增加jasypt配置
jasypt:
encryptor:
# 鹽值
password: 123
# 指定加密方式
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
property:
# 標(biāo)識為加密屬性的前綴
prefix: ENC(
# 標(biāo)識為加密屬性的后綴
suffix: )
這樣還是會把加密信息暴漏,推薦使用配置類更加安全
方式二:通過配置類添加加密配置
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class EncryptConfig {
/**
* 自定義 StringEncryptor,覆蓋默認(rèn)的 StringEncryptor
* bean 名稱是必需的,從 1.5 版開始按名稱檢測自定義字符串加密程序,默認(rèn) bean 名稱為:jasyptStringEncryptor
*
*/
@Bean("jasyptStringEncryptor")
public StringEncryptor jasyptStringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("tarzan");
config.setPoolSize("1");
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
}
第三步:在配置文件application.yml中密文替代明文。
spring:
redis:
##redis 單機(jī)環(huán)境配置
host: ${REDIS_HOST:127.0.0.1}
port: 6379
password: ${REDIS_PASSWORD:ENC(YiSSNr7KKHbglJ0Er+YSOCr22X1Xwgi+NX6eqPARS48=)}
database: 5
ssl: false
data:
mongodb:
database: ${MONGO_DATABASE:work_face_tmzk5}
host: ${MONGO_HOST:127.0.0.1}
username: ${MONGO_USERNAME:tarzan-mongo}
password: ${MONGO_PASSWORD:ENC(XYZ6QH/B1YnSQhTgqtq9tqn/6NIxotNmBDF8ve57Vh0=)}
datasource:
url: jdbc:postgresql://${POSTGRES_HOST:127.0.0.1}:${POSTGRES_PORT:5432}/${POSTGRES_DATABASE:coalface_safety}
username: ${POSTGRES_USERNAME:postgres}
password: ${POSTGRES_PASSWORD:ENC(YiSSNr7KKHbglJ0Er+YSOCr22X1Xwgi+NX6eqPARS48=)}
driver-class-name: org.postgresql.Driver
ENC(YiSSNr7KKHbglJ0Er+YSOCr22X1Xwgi+NX6eqPARS48=) 只能單獨(dú)使用,不能和其他文本拼接,下面是個(gè)錯(cuò)誤例子:
spring:
data:
mongodb:
database: ${MONGO_DATABASE:work_face_szls}
uri: mongodb://glqxzh:ENC(YiSSNr7KKHbglJ0Er+YSOCr22X1Xwgi+NX6eqPARS48=):27017
第四步::啟動類加入開啟加密注解
@EnableEncryptableProperties
@SpringBootApplication(scanBasePackages = AppConstant.BASE_PACKAGES,exclude = {TenantDataSourceConfiguration.class})
public class CoalfaceSafetyApplication {
public static void main (String[] args) {
ApplicationContext context =BladeApplication.run("coalface-safety", CoalfaceSafetyApplication.class, args);
}
完成以上四步即可,啟動后,會自動解密配置信息。
加密工具類
在第三步使用的加密信息,需要工具類生成,代碼如下:
package org.springblade.coalface.utils;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.PBEConfig;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
public class JasyptUtils {
/**
* {@link StringEncryptor} 加解密。
* 同一個(gè)密鑰(secretKey)對同一個(gè)內(nèi)容執(zhí)行加密,生成的密文都是不一樣的,但是根據(jù)根據(jù)這些密文解密成明文都是可以.
* 1、Jasypt 默認(rèn)使用 {@link StringEncryptor} 來解密全局配置文件中的屬性,所以提供密文時(shí),也需要提供 {@link StringEncryptor} 加密的密文
* 2、{@link StringEncryptor} 接口有很多的實(shí)現(xiàn)類,比如常用的 {@link PooledPBEStringEncryptor}
* 3、setConfig(final PBEConfig config):為對象設(shè)置 {@link PBEConfig} 配置對象
* 4、encrypt(final String message):加密內(nèi)容
* 5、decrypt(final String encryptedMessage):解密內(nèi)容
*
* @param secretKey :密鑰。加/解密必須使用同一個(gè)密鑰
* @param message :加/解密的內(nèi)容
* @param isEncrypt :true 表示加密、false 表示解密
* @return
*/
public static String stringEncryptor(String secretKey, String message, boolean isEncrypt) {
PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
pooledPBEStringEncryptor.setConfig(getSimpleStringPBEConfig(secretKey));
String result = isEncrypt ? pooledPBEStringEncryptor.encrypt(message) : pooledPBEStringEncryptor.decrypt(message);
return result;
}
/**
* 設(shè)置 {@link PBEConfig} 配置對象,SimpleStringPBEConfig 是它的實(shí)現(xiàn)類
* 1、所有的配置項(xiàng)建議與全局配置文件中的配置項(xiàng)保持一致,特別是 password、algorithm 等等選項(xiàng),如果不一致,則應(yīng)用啟動時(shí)解密失敗而報(bào)錯(cuò).
* 2、setPassword(final String password):設(shè)置加密密鑰,必須與全局配置文件中配置的保存一致,否則應(yīng)用啟動時(shí)會解密失敗而報(bào)錯(cuò).
* 3、setPoolSize(final String poolSize):設(shè)置要創(chuàng)建的加密程序池的大小.
* 4、setAlgorithm(final String algorithm): 設(shè)置加密算法的值, 此算法必須由 JCE 提供程序支持
* 5、setKeyObtentionIterations: 設(shè)置應(yīng)用于獲取加密密鑰的哈希迭代次數(shù)。
* 6、setProviderName(final String providerName):設(shè)置要請求加密算法的安全提供程序的名稱
* 7、setSaltGeneratorClassName:設(shè)置 Sal 發(fā)生器
* 8、setIvGeneratorClassName:設(shè)置 IV 發(fā)生器
* 9、setStringOutputType:設(shè)置字符串輸出的編碼形式??捎玫木幋a類型有 base64、hexadecimal
*
* @param secretKey
* @return
*/
private static SimpleStringPBEConfig getSimpleStringPBEConfig(String secretKey) {
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(secretKey);
config.setPoolSize("1");
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64");
return config;
}
public static void main(String[] args) throws Exception {
String message = "@5Rd!TC2CBA";
String password = "tarzan";
//一個(gè)同樣的密碼和秘鑰,每次執(zhí)行加密,密文都是不一樣的。但是解密是沒問題的。
String jasyptEncrypt = stringEncryptor(password, message, true);
System.out.println(jasyptEncrypt);
String jasyptEncrypt1 = stringEncryptor(password, jasyptEncrypt, false);
System.out.println(jasyptEncrypt1);
}
}
修改主方法中的message和password 即可。message為需要加密的文本,password 是加密鹽值。
當(dāng)然你也可以講加密方法封裝成接口調(diào)用,示例代碼如下:
@Resource
private StringEncryptor stringEncryptor;
/**
* http://localhost:8080/jasypt/encryptor?message=12日下午17點(diǎn)執(zhí)行任務(wù)&isEncrypt=true
* http://localhost:8080/jasypt/encryptor?message=702EAA3755766C567F62E83273681A90DC684B6AFADD5CD84691778DAF4A1466E13CE0720E8BABC06081A5D6DBD90EA1&isEncrypt=false
* 在線使用 {@link StringEncryptor} 加解密消息。
*
* @param message 加/解密的內(nèi)容
* @param isEncrypt true 表示加密、false 表示解密
* @return
*/
@GetMapping("jasypt/encryptor")
public ObjectNode encrypt(@RequestParam String message, @RequestParam boolean isEncrypt) {
JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
String encrypt = isEncrypt ? stringEncryptor.encrypt(message) : stringEncryptor.decrypt(message);
ObjectNode objectNode = nodeFactory.objectNode();
objectNode.put("code", 200);
objectNode.put("data", encrypt);
return objectNode;
}
到此這篇關(guān)于SpringBoot保護(hù)配置文件中敏感信息的保姆級教程的文章就介紹到這了,更多相關(guān)SpringBoot保護(hù)配置文件敏感信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jmeter多臺機(jī)器并發(fā)請求實(shí)現(xiàn)壓力性能測試
這篇文章主要介紹了Jmeter多臺機(jī)器并發(fā)請求實(shí)現(xiàn)壓力性能測試,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
用Rational Rose逆向工程(java)生成類圖(教程和錯(cuò)誤解決)
Rational Rose有個(gè)很方便的功能,將項(xiàng)目中的JAVA代碼自動轉(zhuǎn)換成UML類圖2013-02-02
SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼
本文主要介紹了SpringBoot實(shí)現(xiàn)熱部署Community的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
SpringBoot接收參數(shù)所有方式總結(jié)
這篇文章主要介紹了SpringBoot接收參數(shù)所有方式總結(jié),文中通過代碼示例和圖文結(jié)合的方式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
spring中@Autowired自動注入依賴項(xiàng)的使用
當(dāng)使用@Autowired注解時(shí),它可以自動注入依賴項(xiàng),例如其他類的實(shí)例,本文就來詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09

