当前位置:网站首页>配置文件加密(Jasypt的简单使用)
配置文件加密(Jasypt的简单使用)
2022-06-28 14:08:00 【华为云】
title: 配置文件加密(Jasypt的简单使用)
categories: Jasypt
tags: Jasypt
需求引入
当我们需要对SpringBoot项目的配置文件中的信息进行加密时,如数据库密码等,此时我们可以使用Jasypt来实现。
解决方案
第一步,引入Jasypt依赖
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version></dependency>第二步,编写工具类
JasyptUtils:
package com.keafmd.springdemo.utils;import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;/** * Keafmd * * @ClassName: JasyptUtils * @Description: * @author: 牛哄哄的柯南 * @date: 2022-04-25 11:18 */public class JasyptUtils { /** * Jasypt生成加密结果 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.password * @param value 待加密值 * @return */ public static String encryptPwd(String password, String value) { PooledPBEStringEncryptor encryptOr = new PooledPBEStringEncryptor(); encryptOr.setConfig(cryptOr(password)); String result = encryptOr.encrypt(value); return result; } /** * 解密 * * @param password 配置文件中设定的加密密码 jasypt.encryptor.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; } /** * @param password salt * @return */ public static SimpleStringPBEConfig cryptOr(String password) { SimpleStringPBEConfig config = new SimpleStringPBEConfig(); config.setPassword(password); config.setAlgorithm(StandardPBEByteEncryptor.DEFAULT_ALGORITHM); config.setKeyObtentionIterations("1000"); config.setPoolSize("1"); config.setProviderName(null); config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); config.setStringOutputType("base64"); return config; } public static void main(String[] args) { // 加密 // 盐值替换成自己熟悉的口令,此口令为解密密钥,需要妥善保管。 // 盐值也需要在第三步写入配置文件 System.out.println(encryptPwd("keafmd", "123456")); }}第三步,修改配置文件
- 把盐值写入配置文件
- 把明文密码替换为工具类生成的密文,并用ENC()包着
eg:
# 连接数据源spring: datasource: username: root password: ENC(aXcmM77CmYgj67mBxQgI2A==) url: jdbc:mysql://XXXX:XX/tt?useUnicode=true&characterEncoding=UTF-8&useSSL=false jasypt: encryptor: password: keafmd以上就是配置文件加密(Jasypt的简单使用)的全部内容
版权声明:
原创博主:牛哄哄的柯南
个人博客链接:https://www.keafmd.top/
看完如果对你有帮助,感谢点击下面的==一键三连==支持!
[哈哈][抱拳]

加油!
共同努力!
Keafmd
都看到这里了,下面的内容你懂得,让我们共同进步!
边栏推荐
- Embedded design and development project - liquid level detection and alarm system
- China Radio and television 5g package is coming, lower than the three major operators, but not as low as expected
- G : 最大流问题
- BERT为何无法彻底干掉BM25??
- DevEco Studio 3.0编辑器配置技巧篇
- Summary of 2021 computer level III database
- New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
- 《畅玩NAS》家庭 NAS 服务器搭建方案「建议收藏」
- Who is the main body of the waiting insurance record? Record in the local network security, right?
- Navicat premium 16 permanent crack activation tool and installation tutorial (available for personal test)
猜你喜欢
随机推荐
Regular matching numbers, English and English symbols
正则匹配数字,英文以及英文符号
同花顺上开户是安全的吗
Deveco studio 3.0 editor configuration tips
Connected to rainwater series problems
What are the products of increased life insurance?
Reverse a stack with recursive functions and stack operations only
Npoi export excel and download to client
Talk about exception again -- what happens when an exception is thrown?
ThreadLocal的简单理解
Solving Hanoi Tower problem
Nature子刊 | 绘制植物叶际菌群互作图谱以建立基因型表型关系
Operation and maintenance thinking | do you know the relationship between CMDB and monitoring?
推荐四款可视化工具,解决 99% 的可视化大屏项目!
Black apple installation tutorial OC boot "suggestions collection"
MySQL从库Error:“You cannot ‘Alter‘ a log table...“
Double buffer drawing
Zhongang mining focuses on the fluorine chemical industry and lays out the new energy industry chain
New drug discovery methods, AstraZeneca team improves ab initio molecular design through course learning
i++ , ++i









