当前位置:网站首页>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()等。
当然了,你的盐和密码一定要分开存储,也就是说,加密后的密码配置在配置文件中,加密的盐可以放在启动参数上。
边栏推荐
- js判断数组中是否存在某个元素(四种方法)
- RHCSA8
- JPA规范总结和整理
- Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
- FPGA learning notes: vivado 2019.1 add IP MicroBlaze
- 面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
- Fragmented knowledge management tool memos
- [深度学习论文笔记]TransBTSV2: Wider Instead of Deeper Transformer for Medical Image Segmentation
- AVC1与H264的区别
- Go pointer
猜你喜欢
How to realize batch sending when fishing
Talk about seven ways to realize asynchronous programming
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
山东大学暑期实训一20220620
Write API documents first or code first?
Android本地Sqlite数据库的备份和还原
龙芯派2代烧写PMON和重装系统
Backup and restore of Android local SQLite database
jenkins安装
Record in-depth learning - some bug handling
随机推荐
什么是网络端口
Talk about seven ways to realize asynchronous programming
leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
go 数组与切片
mysql econnreset_ Nodejs socket error handling error: read econnreset
网络安全-HSRP协议
先写API文档还是先写代码?
数据泄露怎么办?'华生·K'7招消灭安全威胁
RHCSA10
Android本地Sqlite数据库的备份和还原
Record in-depth learning - some bug handling
"Baidu Cup" CTF competition in September, web:upload
Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
Could not set property ‘id‘ of ‘class XX‘ with value ‘XX‘ argument type mismatch 解决办法
[深度学习论文笔记]使用多模态MR成像分割脑肿瘤的HNF-Netv2
UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xe6 in position 76131: invalid continuation byt
FPGA learning notes: vivado 2019.1 add IP MicroBlaze
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)
碎片化知识管理工具Memos
Talking about fake demand from takeout order