惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
L
LangChain Blog
博客园 - Franky
美团技术团队
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
小众软件
小众软件
Y
Y Combinator Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News

祈雨的笔记

安全多方计算MPC spark原理解析 kueue执行源码分析 spark on k8s执行源码分析 spark-operator源码解析 系统压测遇到的缓存击穿问题 我的世界PC与安卓联机 蚂蚁金服流量投放平台的AIG改造 G1大对象致Old区占用率高 日志打印导致接口响应率下跌分析 Groovy加载类导致OOM分析 ERROR日志打印导致CPU满载 记OceanBase死锁超时 应用发版期间服务响应超时 Ark Serverless初探 系统优化复盘一二三 The user specified as a definer does not exist Kong网关初探 API网关选型调研 CPU火焰图常用工具 配置中心选型调研 root操作Nginx导致用户组错误 基于Proxifier使用代理 FastJSON字段智能匹配踩坑 Nacos初探 记一次Nginx服务器CPU满荷载故障 基于券系统分库分表的思考 limit不参与SQL成本计算致索引失效 Linux常用性能监控命令 golang低版本http2偶现400
springboot(14)配置文件加密解密
祈雨的笔记 · 2018-05-21 · via 祈雨的笔记

1、介绍

jasypt-spring-boot

jasypt可以在springboot注入property和yml配置文件中的值之前,将配置文件中的值先预先处理的工具。可以用来实现对数据库账号密码等敏感信息密文解密的功能。

2、Maven依赖

1
2
3
4
5
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>

3、注册解密Bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Configuration
public class EncryptionPropertyConfig {

@Bean(name="encryptablePropertyResolver")
public EncryptablePropertyResolver encryptablePropertyResolver() {
return new EncryptionPropertyResolver();
}

class EncryptionPropertyResolver implements EncryptablePropertyResolver {

@Override
public String resolvePropertyValue(String value) {
if(StringUtils.isBlank(value)) {
return value;
}

if(value.startsWith("DES@")) {
return resolveDESValue(value.substring(4));
}

return value;
}

private String resolveDESValue(String value) {

return DESUtil.getDecryptString(value);
}

}
}

4、测试

4.1、property配置文件

1
2
3
4
5
6
# 127.0.0.1的密文为e3zcSlYS29N0Y3i+mVdkgQ==
datasource.host=DES@e3zcSlYS29N0Y3i+mVdkgQ==
# 3306的密文为S6mBLsaSBEw=
datasource.port=DES@S6mBLsaSBEw=
datasource.database=test
datasource.url=jdbc:mysql://${datasource.host}:${datasource.port}/${datasource.database}?useUnicode=true&amp;characterEncoding=utf8

4.2、注入

1
2
3
4
5
6
7
8
9
10
11
12
13
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {

@Value("${datasource.url}")
private String url;

@Test
public void testJasypt() {
System.out.println(url);
}

}

4.3、输出

1
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=utf8