国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

SpringBoot 集成 Jasypt 對數據庫加密以及踩坑的記錄分享

瀏覽:4日期:2023-05-03 13:50:22

前言

密碼安全是非常重要的,因此我們在代碼中往往需要對密碼進行加密,以此保證密碼的安全

加依賴

<!-- jasypt --><dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version></dependency>

加配置

# jasypt 密碼加密配置jasypt: encryptor: # 加密鹽值 password: jasypt # 加密算法設置 3.0.0 以后 algorithm: PBEWithMD5AndDES iv-generator-classname: org.jasypt.iv.NoIvGenerator

PS:可以看到配置中特意配置了加密算法,原因是官方在 3.0.0 以后更改了加密算法,所以假如你不設置的話,使用網上的方法加密出來的密碼啟動就會報錯,如圖:

SpringBoot 集成 Jasypt 對數據庫加密以及踩坑的記錄分享

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

SpringBoot 集成 Jasypt 對數據庫加密以及踩坑的記錄分享

版本在 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生成加密結果 * @param password 配置文件中設定的加密鹽值 * @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 配置文件中設定的加密鹽值 * @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); }}

數據庫配置解密

用官方提供的保留字 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

到此這篇關于SpringBoot 集成 Jasypt 對數據庫加密以及踩坑的記錄分享的文章就介紹到這了,更多相關SpringBoot 集成 Jasypt 對數據庫加密內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 乐陵市| 昆明市| 东兴市| 毕节市| 特克斯县| 邳州市| 昌黎县| 缙云县| 甘孜县| 册亨县| 赞皇县| 玉屏| 合江县| 金塔县| 邹城市| 隆昌县| 林西县| 株洲县| 瑞昌市| 陇南市| 周口市| 黄陵县| 丹阳市| 永兴县| 汨罗市| 张掖市| 岗巴县| 鄯善县| 阿坝| 京山县| 扬中市| 时尚| 金沙县| 固原市| 奉化市| 拜城县| 新民市| 洛隆县| 溧水县| 辉县市| 正宁县|