SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解
一、簡介
在后端開發(fā)中有很多敏感信息,比如數(shù)據(jù)庫用戶名密碼,第三方 Apikey,云服務(wù)商的 secretKey 等、如果不希望用明文在 application.yml 配置的,可以使用 jasypt 加密這些字段。
還有很重要的一點(diǎn),如果你自己開源一些東西,將代碼上傳一些代碼托管平臺(tái),肯定需要隱藏敏感信息,用 jasypt 加密可以簡化每次上傳下拉代碼修改敏感信息。
官方文檔, 官方文檔使用方法描述得很清楚適用于各種情況,下面我簡單記錄一下加密 MySQL 用戶名密碼方法
二、導(dǎo)入依賴
<dependencies> <!-- jasypt 敏感數(shù)據(jù)加密,如:數(shù)據(jù)庫密碼,阿里云短信服務(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); } }
可以看到加密過后的字符串如下
四、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)類測試
查詢 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)過上面的操作,我們已經(jīng)將敏感信息加密
到此這篇關(guān)于SpringBoot整合jasypt實(shí)現(xiàn)敏感信息的加密詳解的文章就介紹到這了,更多相關(guān)SpringBoot敏感信息加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot使用jasypt對(duì)配置文件加密加密數(shù)據(jù)庫連接的操作代碼
- SpringBoot整合Jasypt實(shí)現(xiàn)配置加密的步驟詳解
- Springboot集成Jasypt實(shí)現(xiàn)配置文件加密的方法
- 微服務(wù)SpringBoot整合Jasypt加密工具的場景分析
- SpringBoot集成Jasypt敏感信息加密的操作方法
- springboot 項(xiàng)目使用jasypt加密數(shù)據(jù)源的方法
- Jasypt對(duì)SpringBoot配置文件加密
- jasypt 集成SpringBoot 數(shù)據(jù)庫密碼加密操作
- SpringBoot 集成 Jasypt 對(duì)數(shù)據(jù)庫加密以及踩坑的記錄分享
- 基于Jasypt對(duì)SpringBoot配置文件加密
- 在SpringBoot中通過jasypt進(jìn)行加密解密的方法
- SpringBoot使用Jasypt對(duì)配置文件和數(shù)據(jù)庫密碼加密
相關(guān)文章
Java實(shí)現(xiàn)復(fù)原IP地址的方法
這篇文章主要介紹了Java實(shí)現(xiàn)復(fù)原IP地址的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02Spring Boot中操作使用Redis實(shí)現(xiàn)詳解
Spring Boot與Redis結(jié)合使用,通過使用Spring Data Redis來實(shí)現(xiàn)對(duì)Redis的操作,實(shí)現(xiàn)數(shù)據(jù)緩存和高效存儲(chǔ),提高應(yīng)用程序的性能和響應(yīng)速度??梢岳肧pring Boot自帶的Redis Starter方便地集成和配置Redis2023-04-04IDEA2020如何打開Run Dashboard的方法步驟
這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07使用@Validated 和 BindingResult 遇到的坑及解決
這篇文章主要介紹了使用@Validated 和 BindingResult 遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10詳解Spring Boot 配置多個(gè)RabbitMQ
本篇文章主要介紹了Spring Boot 配置多個(gè)RabbitMQ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06java 使用DecimalFormat進(jìn)行數(shù)字的格式化實(shí)例詳解
這篇文章主要介紹了java 使用DecimalFormat進(jìn)行數(shù)字的格式化實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06java中實(shí)現(xiàn)分頁的幾種常見方式總結(jié)
在項(xiàng)目中經(jīng)常會(huì)查詢大量數(shù)據(jù),這就要用到分頁展示,下面這篇文章主要給大家介紹了關(guān)于java中實(shí)現(xiàn)分頁的幾種常見方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12