jasypt加密_Spring Boot 配置 配置信息加密

点击上方☝ SpringForAll社区 ,轻松关注! 及时获取有趣有料的技术文章

68db5f42-3a2f-eb11-8da9-e4434bdf6706.png

▶ Spring Boot 依赖与配置

Maven 依赖

        <dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>

<dependency>
<groupId>com.github.ulisesbocchiogroupId>
<artifactId>jasypt-spring-boot-starterartifactId>
<version>2.1.1version>
dependency>

▶ 使用说明

假设有配置项 com.anoyi.custom.name=anoyi 不能明文显示,则可以使用 jasypt 的 PBEWithMD5AndDES 算法加密算法进行如下配置:

com.anoyi.custom.name=ENC(TqrnYZn55aFVwnSo2TrbFA==)
jasypt.encryptor.password=anoyi
  • jasypt.encryptor.password 为自定义值,用此密码加密的明文,需要用此密码解密密文

  • ENC(...) 为 jasypt 提供的加密标识,Spring Boot 服务启动时,加载各种 properties 时会依据此标识判断是否解密赋值,可自定义

  • TqrnYZn55aFVwnSo2TrbFA== 为明文字符串 anoyi 通过密码 anoyi 加密后得到的值,此值不唯一,即同一明文通过同一密码加密会得到不同的值

▶ 配置说明

基于 Password 的加密配置

参数必填默认值
jasypt.encryptor.passwordTrue-
jasypt.encryptor.algorithmFalsePBEWithMD5AndDES
jasypt.encryptor.keyObtentionIterationsFalse1000
jasypt.encryptor.poolSizeFalse1
jasypt.encryptor.providerNameFalseSunJCE
jasypt.encryptor.providerClassNameFalsenull
jasypt.encryptor.saltGeneratorClassnameFalseorg.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.ivGeneratorClassnameFalseorg.jasypt.salt.NoOpIVGenerator
jasypt.encryptor.stringOutputTypeFalsebase64
jasypt.encryptor.proxyPropertySourcesFalsefalse

最新版的 jasypt 还支持非对称加密、自定义加密器等等功能,更多信息:

MORE :https://github.com/ulisesbocchio/jasypt-spring-boot

▶ 配置参数加解密

添加依赖

        <dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>

示例加解密字符串 anoyi

@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptTest {

@Autowired
private StringEncryptor jasyptStringEncryptor;

@Test
public void encrypt() {
String encryptStr = jasyptStringEncryptor.encrypt("anoyi");
System.out.println(encryptStr);
}

@Test
public void decrypt() {
String encryptStr = jasyptStringEncryptor.decrypt("TqrnYZn55aFVwnSo2TrbFA==");
System.out.println(encryptStr);
}

}

▶ Github Demo URL

  • https://github.com/ChinaSilence/spring-boot-demos/tree/master/05%20-%20config%20encrypt

© 著作权归作者所有,转载或内容合作请联系作者

69db5f42-3a2f-eb11-8da9-e4434bdf6706.gif

● 拒绝黑盒应用-Spring Boot 应用可视化监控

● 并发Bug之源有三,请睁大眼睛看清它们

● 史上最轻松入门之Spring Batch - 轻量级批处理框架实践

● Spring Cloud Gateway - 快速开始

● APM工具寻找了一圈,发现SkyWalking才是我的真爱

● Spring Boot 注入外部配置到应用内部的静态变量

● 将 HTML 转化为 PDF新姿势

● Java 使用 UnixSocket 调用 Docker API

● Fastjson致命缺陷

● Service Mesh - gRPC 本地联调远程服务

● 使用 Thymeleaf 动态渲染 HTML

6bdb5f42-3a2f-eb11-8da9-e4434bdf6706.png

6ddb5f42-3a2f-eb11-8da9-e4434bdf6706.gif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值