依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
springboot配置
yml配置
jasypt:
encryptor:
password: saltValue #salt值,密文加盐
spring:
datasource: # 数据库链接
db1:
jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
username: root #也可以加密用户名,依然是ENC()格式,这里没有进行加密
password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+) #加密了密码,ENC()括号内为密文
driver-class-name: com.mysql.cj.jdbc.Driver
mapper-locations: classpath*:mapper/otcmapper/*.xml
启动类添加注解:@EnableEncryptableProperties
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEncryptableProperties
@EnableScheduling
//@EnableAsync
public class SpBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpBatchApplication.class, args);
}
}
通过明文获取加密的值
cmd在自己的maven仓库目录下执行命令,(要保证依赖下载下来了)
解释:
input:文字的明文
password:加密的盐值(可随意,必须=jasypt:encryptor:password: saltValue)
algorithm:PBEWithMD5AndDES(默认算法)
java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密码明文" password=saltValue algorithm=PBEWithMD5AndDES
执行后输出结果:OUTPUT就是密文了,把密文替换yml的属性值就行
ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: 密码明文
password: saltValue
----OUTPUT----------------------
OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+
启动springboot就会自动解密了
通过密文和盐值解密得到明文
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=saltValue algorithm=PBEWithMD5AndDES