SpringBoot实战(二)——配置文件内容加密jasypt

    使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些。打开application.properties或application.yml,比如mysql登陆密码,redis登陆密码以及第三方的密钥等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。

    jasypt由一个国外大神写了一个springboot下的工具包,下面直接看代码:

    1、maven依赖引入:

<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.14</version>
		</dependency>

    2、application.properties配置文件中增加如下内容(加解密时使用):

jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

    3、里面main方法或测试生成秘钥:

//这是JUnit的注解,通过这个注解让SpringJUnit4ClassRunner这个类提供Spring测试上下文。  
@RunWith(SpringJUnit4ClassRunner.class)  
//这是Spring Boot注解,为了进行集成测试,需要通过这个注解加载和配置Spring应用上下  
@SpringBootTest(classes = Application.class)  
@WebAppConfiguration 
public class EncryptorTest {
	
	@Autowired
	StringEncryptor encryptor;
	
	@Test
	public void getPass() {
		String name = encryptor.encrypt("DBusername");
		String password = encryptor.encrypt("DBpassword");
        System.out.println(name+"----------------"); 
        System.out.println(password+"----------------"); 
        Assert.assertTrue(name.length() > 0);
        Assert.assertTrue(password.length() > 0);
	}

}

    4、将上面生成的name和password替换配置文件中的数据库账户和密码,替换后如下:

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://1.1.1.1:3306/xxxx?characterEncoding=utf8&useSSL=true
spring.datasource.username=ENC(zj4OUZ3/mmHV0JOgCdg8qQ==)
spring.datasource.password=ENC(UBtTQGk1xbdmhff+UUoT6g==)

简单四步完成加密操作。。。。。。。。。。。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值