提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前面叙述了数据传递过程中的加密,需要使用到aop对result和request进行加密和解密。现在需要对服务器的中间件进行保护设置,在配置文件中尽量不要暴露出来服务器件的密码账号。需要对配置文件进行加密处理
jasypt是专门加密各种配置文件的依赖包
com.github.ulisesbocchio jasypt-spring-boot-starter 3.0.3
jasypt有多种版本,并且包之间的依赖问题比较多
如下(yml配置):
jasypt:encryptor:algorithm: PBEWithMD5AndDES #算法-固定写法一般没人改password: 1234qweriv-generator-classname: org.jasypt.iv.NoIvGenerator #设置初始向量IV生成器的类名
使用代码生成密钥
public class JasyptUtil {/*** 加密方法* @param salt 盐值* @param targetString 待加密字符串* @return 密文*/public static String encrypt(String salt, String targetString) {BasicTextEncryptor encryptor = new BasicTextEncryptor();encryptor.setPassword(salt);return encryptor.encrypt(targetString);}/*** 解密方法* @param salt 盐值* @param targetString 待解密字符串* @return 明文*/public static String decrypt(String salt,String targetString) {BasicTextEncryptor encryptor = new BasicTextEncryptor();encryptor.setPassword(salt);return encryptor.decrypt(targetString);}public static void main(String[] args) {String salt = "1234qwer";String password = "xxxxx";// 进行加密操作String encryptString1 = encrypt(salt, password);// 进行解密操作String decryptString1 = decrypt(salt, encryptString1);// 输出明文和密文System.out.println("encryptString1="+encryptString1);System.out.println("decryptString1="+decryptString1);}
}
使用jar包生成密钥
java -cp D:\repository\org\jasypt\jasypt\1.9.3\jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="xxxxx" password=1234qwer algorithm=PBEWithMD5AndDES
在yml配置文件中修改密钥:
password: ENC(KMMwXkYtccOvU+mMOXXojP7Tif1Ja9fN)
ENC是jaspty中默认的,可以设置配置项自定义设置
在idea中设置启动项configuration,添加环境变量
nohup java -Djasypt.encrypt.password=1234qwer -jar xxxxx.jar > nohup.log &
版本问题,解决冲突
如果错误信息是缺少jce安全插件,那么需要下载jce_policy-8的zip包去放入到JDK下的jre/lib/security下