Spring Boot -- 配置文件内容加密jasypt

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

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

    1、maven依赖引入:

[html]  view plain  copy
  1. <!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->  
  2.         <dependency>  
  3.             <groupId>com.github.ulisesbocchio</groupId>  
  4.             <artifactId>jasypt-spring-boot-starter</artifactId>  
  5.             <version>1.14</version>  
  6.         </dependency>  

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

[html]  view plain  copy
  1. jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7  

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

[java]  view plain  copy
  1. //这是JUnit的注解,通过这个注解让SpringJUnit4ClassRunner这个类提供Spring测试上下文。    
  2. @RunWith(SpringJUnit4ClassRunner.class)    
  3. //这是Spring Boot注解,为了进行集成测试,需要通过这个注解加载和配置Spring应用上下    
  4. @SpringBootTest(classes = Application.class)    
  5. @WebAppConfiguration   
  6. public class EncryptorTest {  
  7.       
  8.     @Autowired  
  9.     StringEncryptor encryptor;  
  10.       
  11.     @Test  
  12.     public void getPass() {  
  13.         String name = encryptor.encrypt("DBusername");  
  14.         String password = encryptor.encrypt("DBpassword");  
  15.         System.out.println(name+"----------------");   
  16.         System.out.println(password+"----------------");   
  17.         Assert.assertTrue(name.length() > 0);  
  18.         Assert.assertTrue(password.length() > 0);  
  19.     }  
  20.   
  21. }  

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

[html]  view plain  copy
  1. spring.datasource.driverClassName=com.mysql.jdbc.Driver  
  2. spring.datasource.url=jdbc:mysql://1.1.1.1:3306/xxxx?characterEncoding=utf8&useSSL=true  
  3. spring.datasource.username=ENC(zj4OUZ3/mmHV0JOgCdg8qQ==)  
  4. spring.datasource.password=ENC(UBtTQGk1xbdmhff+UUoT6g==)  

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值