Spring Boot加密应用配置文件敏感信息(jasypt)
maven依赖<dependency><groupId>com.github.ulisesbocchio</groupId><artifactId>jasypt
·
maven依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.16</version>
</dependency>
yml文件加上配置加解密salt
jasypt:
encryptor:
password: passwd #根密码
注:这样写就如同你给门上了锁,但钥匙插在锁孔里!
开发为了方便可以如此,部署时不能这么做,请移步:使用Jasypt对SpringBoot配置文件加密(部署时操作)
使用测试类生成密文
package com.zubus.commonBiz;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import lombok.extern.log4j.Log4j2;
@RunWith(SpringRunner.class)
@SpringBootTest
@Log4j2
public class CommonBizApplicationTests {
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
String mysql_userName = stringEncryptor.encrypt("root");
String mysql_pw = stringEncryptor.encrypt("Hy@12345");
String appid = stringEncryptor.encrypt("wx8e5629e59b406c1a");
String secret = stringEncryptor.encrypt("a2dbfb1643396818374e1d424102ec5b");
String mysql_url = stringEncryptor.encrypt("jdbc:mysql://11.107.23.176/database");
System.out.println("mysql_userName:"+mysql_userName);
System.out.println("mysql_pw:"+mysql_pw);
System.out.println("appid:"+appid);
System.out.println("secret:"+secret);
System.out.println("mysql_url:"+mysql_url);
}
}
在配置文件中使用密文
ENC(密文)
spring:
datasource:
common-biz:
jdbc-url: ENC(gbSChEP7H3NBfCWsT24DuK6NO1cAVSKRABOmpv82k1keGdwoRCjWvJnwIZ94nLzvu9Ix7M=)
username: ENC(pCe0TYU7AhdLwXHz4sQUHdw==)
password: ENC(vXt/1zCHA4v2MSxfIaDaUOo6H3+DhSqID)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献1条内容
所有评论(0)