springboot中使用jasypt对application配置文件中密码进行加密处理

1、在pom中增加依赖:


   com.github.ulisesbocchio
   jasypt-spring-boot-starter
   1.12

2、在测试类test中使用jasypt对密码使用固定秘钥进行加密,也可以测试解密

/**
 * @author zhq
 * @date 2018/1/29 17:39
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class EncryptTest {

    @Autowired
    StringEncryptor stringEncryptor;

    @Test
    public void encryptTest() {
        String result = stringEncryptor.encrypt("querytest!1");
        System.out.println(result);
    }

    @Test
    public void decryptTest() {
        String result = "AyMqfrdCVVTxW04J+8xsbejxj8sME/Lg";
        result = stringEncryptor.decrypt(result);
        System.out.println(result);
    }

}

3、在配置文件application.yml中使用密文作为数据库密码,配置秘钥

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://数据库地址?characterEncoding=utf-8&allowMultiQueries=true
    username: 用户名
    password: ENC(加密之后的密文)
    max-active: 50
    initial-size: 10
    max-wait: 60000
    max-idle: 20
    min-idle: 10
    validation-query: select 1 #validationQuery是检查时用的sql语句
    test-while-idle: true
    test-on-borrow: true
    test-on-return: false
    time-between-eviction-runs-millis: 60000 #隔多少时间回收废弃连接   一般比minEvictableIdleTimeMillis    min-evictable-idle-time-millis: 300000

jasypt:
  encryptor:
    password: 加密时的秘钥

你可能感兴趣的:(spring,boot,编程)