当前位置:网站首页>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()等。
当然了,你的盐和密码一定要分开存储,也就是说,加密后的密码配置在配置文件中,加密的盐可以放在启动参数上。
边栏推荐
- Idea设置方法注释和类注释
- leetcode 10. Regular expression matching regular expression matching (difficult)
- 同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
- Personal component - message prompt
- Shandong University Summer Training - 20220620
- 面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
- Backup and restore of Android local SQLite database
- 什么是网络端口
- "Baidu Cup" CTF competition in September, web:upload
- Notion 类笔记软件如何选择?Notion 、FlowUs 、Wolai 对比评测
猜你喜欢
Sorry, we can't open xxxxx Docx, because there is a problem with the content (repackaging problem)
内网穿透工具 netapp
[深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
蜀天梦图×微言科技丨达梦图数据库朋友圈+1
LB10S-ASEMI整流桥LB10S
Lb10s-asemi rectifier bridge lb10s
Huawei push service content, read notes
南理工在线交流群
[notes of in-depth study paper]uctransnet: rethink the jumping connection in u-net from the perspective of transformer channel
Jenkins installation
随机推荐
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
Integer ==比较会自动拆箱 该变量不能赋值为空
Although the volume and price fall, why are the structural deposits of commercial banks favored by listed companies?
【 script secret pour l'utilisation de MySQL 】 un jeu en ligne sur l'heure et le type de date de MySQL et les fonctions d'exploitation connexes (3)
[MySQL usage Script] catch all MySQL time and date types and related operation functions (3)
MySQL - database query - sort query, paging query
华为推送服务内容,阅读笔记
私有地址有那些
Don't know these four caching modes, dare you say you understand caching?
JS to determine whether an element exists in the array (four methods)
Flutter draws animation effects of wave movement, curves and line graphs
What happened to the communication industry in the first half of this year?
Shandong University Summer Training - 20220620
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
How to realize batch sending when fishing
MySQL get time
面试官灵魂拷问:为什么代码规范要求 SQL 语句不要过多的 join?
南理工在线交流群
同事半个月都没搞懂selenium,我半个小时就给他整明白!顺手秀了一波爬淘宝的操作[通俗易懂]
从外卖点单浅谈伪需求