跳到主要内容

硬编码方案

硬编码问题

Jasypt maven 插件

  1. 添加依赖
<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
  1. 配置插件
<!--jasypt-maven-plugin-->
<plugin>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<path>file:src/main/resources/application.yml</path>
</configuration>
</plugin>
  1. 配置文件
server:
port: 9527

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url-helper: DEC(localhost:3306/test)
url: jdbc:mysql://${spring.datasource.url-helper}?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: DEC(ROOT)
password: DEC(123456)
redis:
host: DEC(localhost)
port-helper: DEC(6379)
port: ${spring.redis.port-helper}
password: DEC(123456)
jasypt:
encryptor:
password: DEC(123456)
  1. 执行命令
mvn jasypt:encrypt

或者点击idea中的插件 jasypt -> jasypt:encrypt

  1. 删除配置文件中的jasypt.encryptor.password配置
  2. 打包
  3. 生产运行项目,并设置jasypt.encryptor.password参数 这个参数得和配置文件中设置的密码一致
java -jar target/Jasypt-1.0-SNAPSHOT.jar --jasypt.encryptor.password="DEC(123456)"

或者

java -Djasypt.encryptor.password="DEC(123456)" -jar target/Jasypt-1.0-SNAPSHOT.jar
  1. 访问接口,获取配置文件,得到解密后的数据