1、springboot项目中,引入依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
2、在application.yml中配置jasypt.encryptor.password=yoursalt ,例如
jasypt:
encryptor:
password: turing
如果不想将jasypt的加密盐值直接配置在配置文件中,则也可改为在启动jar命令行中指定程序参数或jvm参数
#启动springboot的jar包,指定程序参数
java -jar xxx.jar --jasypt.encryptor.password=turing
#启动springboot的jar包,指定JVM参数
java -Djasypt.encryptor.password=turing -jar xxx.jar
3、命令行加解密
#直接使用命令对明文进行加密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=turing algorithm=PBEWithMD5AndDES
#直接使用命令对密文进行解密
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="KCQCruXa2BN+StcKVPlkAg==" password=turing algorithm=PBEWithMD5AndDES
4、 在项目业务代码中使用jasypt
5、application.yml中配置敏感信息
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: ENC(KCQCruXa2BN+StcKVPlkAg==) #注意jasypt的密文需要使用ENC()括起来
url: jdbc:mysql://localhost:3306/springmybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1