jasypt-spring-boot使用说明
加密密码
命令:
java -cp F://.m2/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI
input="test123" password=e9fbdb2d3b213c28575c095ae0029e05f40f77ee53ecd24af815bdff5479dd2a
algorithm=PBEWithMD5AndDES
红色部分代表需要加密的密码。命令回显如下(红色部分是加密后的密文):
----ENVIRONMENT-----------------
Runtime: Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 25.121-b13
----ARGUMENTS-------------------
algorithm: PBEWithMD5AndDES
input: test123
password: e9fbdb2d3b213c28575c095ae0029e05f40f77ee53ecd24af815bdff5479dd2a
----OUTPUT----------------------
ZV/jFn9unPE2xHIGJLl7WQ==
Maven依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.14</version>
</dependency>
在程序中设置密文
在程序中设置密文需要使用如下格式:
ENC(密文)
如:
spring.datasource.password=ENC(ZV/jFn9unPE2xHIGJLl7WQ==)
在程序中获取到的spring.datasource.password会自动转换成明文内容(test123)。
配置密文密码
在启动命令中配置 JVM 参数( jasypt.encryptor.password ),注入加密密文的密码。
如:
java -Dfile.encoding=UTF8 -
Djasypt.encryptor.password= e9fbdb2d3b213c28575c095ae0029e05f40f77ee53ecd24af815bdff5479dd2a -jar -
Xmx512m gh_settlement.jar
注:在docker容器中密文的密码会设置成环境变量 JASYPT_PASSWORD ,所以以上命令需要修改为:
java -Dfile.encoding=UTF8 -Djasypt.encryptor.password= ${JASYPT_PASSWORD } -jar -Xmx512m
gh_settlement.jar
参考文档
https://www.ricston.com/blog/encrypting-properties-in-spring-boot-with-jasypt-spring-boot/
https://github.com/ulisesbocchio/jasypt-spring-boot
package com; import org.jasypt.intf.service.JasyptStatelessService; public final class JasyptPBEStringEncryption { /** * <p> * CLI execution method. * </p> * * @param args the command execution arguments */ public static void main(final String[] args) { try { final JasyptStatelessService service = new JasyptStatelessService(); // 需要加密的内容 final String input = "guohuaiGUO4056&";// final String password = "e9fbdb2d3b213c28575c095ae0029e05f40f77ee53ecd24af815bdff5479dd2a"; // final String algorithm = "PBEWithMD5AndDES"; // final String result = service.encrypt(input, password, null, null, algorithm, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); System.out.println("input=" + input); System.out.println("result=" + result); } catch (Throwable t) { t.printStackTrace(); } } }