詳解如何保護SpringBoot配置文件中的敏感信息
如何保護SpringBoot配置文件中的敏感信息
使用過SpringBoot配置文件的朋友都知道,資源文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些。
打開application.properties
或application.yml
,比如 MySql登陸密碼,Redis登陸密碼以及第三方的密鑰
等等一覽無余,這里介紹一個加解密組件,提高一些屬性配置的安全性。
jasypt由一個國外大神寫了一個springboot下的工具包,用來加密配置文件中的信息。
GitHub Demo地址:https://github.com/jeikerxiao/spring-boot2/tree/master/spring-boot-encrypt
下面以數(shù)據(jù)庫用戶名和數(shù)據(jù)庫密碼加密為例進行介紹。
1、pom依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.6</version> <relativePath/> </parent> <groupId>com.jasypt</groupId> <artifactId>spring-boot-jasypt</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-jasypt</name> <description>如何保護 SpringBoot 配置文件中的敏感信息</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.4.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 加密包 --> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
查看最新版本可以到:https://github.com/ulisesbocchio/jasypt-spring-boot
2、配置加/解的密碼
# jasypt加密的密匙 jasypt: encryptor: password: Y6M9fAJQdU7jNp5MW
3、測試用例中生成加密后的秘鑰
package com.jasypt; import org.jasypt.encryption.StringEncryptor; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest class SpringBootJasyptApplicationTests { @Autowired private StringEncryptor encryptor; @Test public void getPass() { String url = encryptor.encrypt("jdbc:mysql://127.0.0.1:3306/test"); String name = encryptor.encrypt("root"); String password = encryptor.encrypt("root"); System.out.println("database url: " + url); System.out.println("database name: " + name); System.out.println("database password: " + password); Assert.assertTrue(url.length() > 0); Assert.assertTrue(name.length() > 0); Assert.assertTrue(password.length() > 0); } }
下面是輸出加密字符串:
database url: CCGZpukXHOy7xPMVm6//IZi3kQxJQmZuFdrje1KtshlZD0IiwrHTQWcB4J4l1qKe
database name: +DcWhoH0RuZck93R2FSEEg==
database password: gTVoDqbBeaTe2+omIsoXIA==
4、將加密后的字符串替換原明文
server: port: 8080 spring: # 數(shù)據(jù)庫相關(guān)配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: CCGZpukXHOy7xPMVm6//IZi3kQxJQmZuFdrje1KtshlZD0IiwrHTQWcB4J4l1qKe username: +DcWhoH0RuZck93R2FSEEg== password: gTVoDqbBeaTe2+omIsoXIA== jpa: hibernate: ddl-auto: update show-sql: true # 返回的api接口的配置,全局有效 jackson: # 如果某一個字段為null,就不再返回這個字段 default-property-inclusion: non_null date-format: yyyy-MM-dd HH:mm:ss serialization: write-dates-as-timestamps: false time-zone: GMT+8 # jasypt加密的密匙 jasypt: encryptor: password: Y6M9fAJQdU7jNp5MW
5、部署時配置salt(鹽)值
為了防止salt(鹽)泄露,反解出密碼,可以在項目部署的時候使用命令傳入salt(鹽)值:
$ java -jar xxx.jar -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW
或者在服務(wù)器的環(huán)境變量里配置,進一步提高安全性。
打開/etc/profile
文件
$ vim /etc/profile
在profile文件末尾插入salt(鹽)變量
$ export JASYPT_PASSWORD = Y6M9fAJQdU7jNp5MW
編譯,使配置文件生效
$ source /etc/profile
運行
$ java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
到此這篇關(guān)于詳解如何保護SpringBoot配置文件中的敏感信息的文章就介紹到這了,更多相關(guān)SpringBoot保護配置文件敏感信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring @Profiles和@PropertySource實現(xiàn)根據(jù)環(huán)境切換配置文件
這篇文章主要介紹了spring @Profiles和@PropertySource根據(jù)環(huán)境切換配置文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11IDEA?一直scanning?files?to?index的四種完美解決方法(VIP典藏版)
這篇文章主要介紹了IDEA?一直scanning?files?to?index的四種完美解決方法(VIP典藏版),推薦第四種方法,第四種方法摸索研究后得出,親測好用,需要的朋友參考下吧2023-10-10如何解決SpringBoot啟動時無法加載配置文件或環(huán)境變量問題
文章主要介紹了在Spring Boot項目中遇到配置文件加載失敗和資源目錄圖標(biāo)異常的問題,并提供了詳細(xì)的解決步驟,解決方法包括在pom.xml文件中添加特定配置,確保資源目錄順序正確,以及注意節(jié)點的正確使用,通過這些步驟,可以有效解決資源加載問題,提高開發(fā)效率2024-12-12Spring Cloud Feign文件傳輸?shù)氖纠a
微服務(wù)中通常使用 Feign 作為服務(wù)消費者,那么如何使用 Feign 接口傳輸文件呢?這篇文章主要介紹了Spring Cloud Feign文件傳輸?shù)氖纠a,感興趣的小伙伴們可以參考一下2018-06-06springboot2.3.1替換為其他的嵌入式servlet容器的詳細(xì)方法
這篇文章主要介紹了springboot2.3.1替換為其他的嵌入式servlet容器的方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07idea打開項目后無法顯示目錄結(jié)構(gòu),只能顯示.iml文件問題
這篇文章主要介紹了idea打開項目后無法顯示目錄結(jié)構(gòu),只能顯示.iml文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Java線上問題排查神器Arthas實戰(zhàn)原理解析
原先我們Java中我們常用分析問題一般是使用JDK自帶或第三方的分析工具如jstat、jmap、jstack、?jconsole、visualvm、Java?Mission?Control、MAT等,還有一款神器Arthas工具,可幫助程序員解決很多繁瑣的問題,感興趣的朋友一起看看吧2022-01-01