[TOC]
#### 1.pom文件引入依赖
````
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
````
#### 2.yml配置本地salt
````
#加密salt
jasypt:
encryptor:
password: xxx #开发环境暴露,生产环境启动传参
````
#### 3.加密数据源
````
package com.zt.controller;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootEsApplicationTests {
@Autowired
StringEncryptor encryptor;
@Test
void contextLoads() {
String dbUrl = encryptor.encrypt("http://xxx");
//System.out.println(dbUrl);
String username = encryptor.encrypt("xxxx");
//System.out.println(username);
String password = encryptor.encrypt("xxxx");
System.out.println(password);
}
}
````
#### 4.ENC规则、替换加密数据,配置完本地启动即可
````
# 主库数据源
master:
url: jdbc:mysql://xxx:3306/table?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: ENC(zrGwWI/r0xlN8pLKBmb0Fo==)
password: ENC(p3P5+Mr9/EItpA2ZroTDGh3UAPKzqesFjIhqsLE=)
````
#### 5.生产模式删除暴露的salt,使用启动参数传入
````
java -jar -Djasypt.encryptor.password=xxx java.jar
````
jasypt数据源加密
最新推荐文章于 2024-07-27 17:56:15 发布