当前位置:网站首页>jasypt配置文件加密|快速入门|实战
jasypt配置文件加密|快速入门|实战
2022-07-05 13:34:00 【步尔斯特】
Jasypt (Java Simplified Encryption) 是一个 java 库,它允许开发人员以最小的成本将基本的加密功能添加到项目中,而无需深入了解密码学的工作原理。
引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
生效
如果是为spring boot项目的配置文件加密,则无需额外配置,启动类上的@SpringBootApplication自动会将其注入程序,使其生效。
作用域
指定配置文件的作用域,其他配置文件不受jasypt影响
@Configuration
@EncryptablePropertySources({
@EncryptablePropertySource("classpath:sentinel-1.properties"),
@EncryptablePropertySource("classpath:sentinel-2.properties")
})
应用
工具类
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
/** * @author issavior */
public class JasyptUtil {
/** * org.jasypt.encryption.StringEncryptor对象 */
private static StringEncryptor stringEncryptor = null;
public static StringEncryptor getInstance(String secretKey) throws Exception {
if (secretKey == null || secretKey.trim().equals("")) {
System.out.println("秘钥不能为空!");
throw new Exception("org.jasypt.encryption.StringEncryptor秘钥不能为空!");
}
if (stringEncryptor == null) {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
// 这个秘钥必须是我们自己定义
config.setPassword(secretKey);
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);
stringEncryptor = encryptor;
}
return stringEncryptor;
}
public static void main(final String[] args) {
// 秘钥字符串
String secretKey = "[email protected]#";
// 待加密的明文密码
String pass = "123456";
try {
StringEncryptor stringEncryptor = JasyptUtil.getInstance(secretKey);
String encryptPass = stringEncryptor.encrypt(pass);
System.out.println("【" + pass + "】被加密成【" + encryptPass + "】");
String clearPass = stringEncryptor.decrypt(encryptPass);
System.out.println("【" + encryptPass + "】被解密成【" + clearPass + "】");
} catch (Exception e) {
e.printStackTrace();
}
}
}
配置
jasypt:
encryptor:
password: 123[email protected]#
my:
# 必须用ENC()包起来,这样jasypt才能识别出来这个密码需要解密再传给应用程序
msg: ENC(PwyGulNOC9YERAC9A6zpH8Da1tn50dtgJ1XBqygkdNoBTkjmENd+F5yJJMp4sthf)
属性一览
进阶
其内部属性都是可以重写的,包括ENC()等。
当然了,你的盐和密码一定要分开存储,也就是说,加密后的密码配置在配置文件中,加密的盐可以放在启动参数上。
边栏推荐
- Shandong University Summer Training - 20220620
- 53. 最大子数组和:给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
- 内网穿透工具 netapp
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET
- Principle and configuration of RSTP protocol
- RHCSA8
- [深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
- 多人合作项目查看每个人写了多少行代码
- French scholars: the explicability of counter attack under optimal transmission theory
猜你喜欢
Principle and configuration of RSTP protocol
Changing JS code has no effect
Catch all asynchronous artifact completable future
Binder communication process and servicemanager creation process
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
C# 对象存储
RHCSA10
CloudCompare——点云切片
Usage, installation and use of TortoiseSVN
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
随机推荐
Don't know these four caching modes, dare you say you understand caching?
RHCSA10
碎片化知识管理工具Memos
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
MMSeg——Mutli-view时序数据检查与可视化
法国学者:最优传输理论下对抗攻击可解释性探讨
龙芯派2代烧写PMON和重装系统
Usage, installation and use of TortoiseSVN
Go pointer
Multi person cooperation project to see how many lines of code each person has written
Android本地Sqlite数据库的备份和还原
Record in-depth learning - some bug handling
Go array and slice
jenkins安装
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
APICloud Studio3 API管理与调试使用教程
【MySQL 使用秘籍】一網打盡 MySQL 時間和日期類型與相關操作函數(三)
ASEMI整流桥HD06参数,HD06图片,HD06应用
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
Personal component - message prompt