当前位置:网站首页>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()等。
当然了,你的盐和密码一定要分开存储,也就是说,加密后的密码配置在配置文件中,加密的盐可以放在启动参数上。
边栏推荐
- With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
- 内网穿透工具 netapp
- Binder communication process and servicemanager creation process
- Nantong online communication group
- Shuttle INKWELL & ink components
- C# 对象存储
- 49. Grouping of alphabetic ectopic words: give you a string array, please combine the alphabetic ectopic words together. You can return a list of results in any order. An alphabetic ectopic word is a
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- Write macro with word
- 程序员成长第八篇:做好测试工作
猜你喜欢

“百度杯”CTF比赛 九月场,Web:Upload

ASEMI整流桥HD06参数,HD06图片,HD06应用

Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?

MMSeg——Mutli-view时序数据检查与可视化

百度杯”CTF比赛 2017 二月场,Web:爆破-2

峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系

Laravel框架运行报错:No application encryption key has been specified

Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution

南理工在线交流群

Mmseg - Mutli view time series data inspection and visualization
随机推荐
Fragmented knowledge management tool memos
Write API documents first or code first?
Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
What are the private addresses
What is a network port
百度杯”CTF比赛 2017 二月场,Web:爆破-2
华为推送服务内容,阅读笔记
C object storage
49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
【Hot100】34. Find the first and last positions of elements in a sorted array
Shu tianmeng map × Weiyan technology - Dream map database circle of friends + 1
“百度杯”CTF比赛 九月场,Web:SQL
Idea set method annotation and class annotation
Nantong online communication group
这18个网站能让你的页面背景炫酷起来
什么是网络端口
碎片化知识管理工具Memos
go 指针
手把手带你入门Apache伪静态的配置
Flutter 3.0更新后如何应用到小程序开发中