无锡网站设计制作,域名注册的网站有哪些,怎样做网站性能优化,深圳百度贴吧springboot使用jasypt对配置文件加密 springboot配置通过明文获取加密的值通过密文和盐值解密得到明文代码封装工具类 dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversio… springboot使用jasypt对配置文件加密 springboot配置通过明文获取加密的值通过密文和盐值解密得到明文代码封装工具类 dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion1.14/version
/dependencyspringboot配置
jasypt:encryptor:password: saltValue #salt值密文加盐
spring:datasource: # 数据库链接db1:jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicodetruecharacterEncodingutf8useSSLfalseallowPublicKeyRetrievaltrueserverTimezoneGMT%2B8username: root #也可以加密用户名,依然是ENC()格式这里没有进行加密password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0) #加密了密码ENC()括号内为密文driver-class-name: com.mysql.cj.jdbc.Drivermapper-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 algorithmPBEWithMD5AndDES默认算法
java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input密码明文 passwordsaltValue algorithmPBEWithMD5AndDES执行后输出结果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加密后的密文 passwordsaltValue algorithmPBEWithMD5AndDES代码封装工具类
public class JasyptUtil {private static final String PBEWITHMD5ANDDES PBEWithMD5AndDES;private static final String PBEWITHHMACSHA512ANDAES_256 PBEWITHHMACSHA512ANDAES_256;/*** * param text 待加密原文* param crack 盐值密钥* return 加密后的字符串* Description: Jasypt加密PBEWithMD5AndDES*/public static String encryptWithMD5(String text, String crack) {
//1.创建加解密工具实例StandardPBEStringEncryptor encryptor new StandardPBEStringEncryptor();
//2.加解密配置EnvironmentStringPBEConfig config new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(crack);encryptor.setConfig(config);
//3.加密return encryptor.encrypt(text);}/*** * param text 待解密原文* param crack 盐值密钥* return 解密后的字符串* Description: Jasypt解密PBEWithMD5AndDES*/public static String decryptWithMD5(String text, String crack) {
//1.创建加解密工具实例StandardPBEStringEncryptor encryptor new StandardPBEStringEncryptor();
//2.加解密配置EnvironmentStringPBEConfig config new EnvironmentStringPBEConfig();config.setAlgorithm(PBEWITHMD5ANDDES);config.setPassword(crack);encryptor.setConfig(config);
//解密return encryptor.decrypt(text);}/*** * param text 待加密的原文* param crack 盐值密钥* return 加密后的字符串* Description: jasypt 加密PBEWITHHMACSHA512ANDAES_256*/public static String encryptWithSHA512(String text, String crack) {
//1.创建加解密工具实例PooledPBEStringEncryptor encryptor new PooledPBEStringEncryptor();
//2.加解密配置SimpleStringPBEConfig config new SimpleStringPBEConfig();config.setPassword(crack);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 为减少配置文件的书写以下都是 Jasypt 3.x 版本配置文件默认配置config.setKeyObtentionIterations(1000);config.setPoolSize(1);config.setProviderName(SunJCE);config.setSaltGeneratorClassName(org.jasypt.salt.RandomSaltGenerator);config.setIvGeneratorClassName(org.jasypt.iv.RandomIvGenerator);config.setStringOutputType(base64);encryptor.setConfig(config);
//3.加密return encryptor.encrypt(text);}/*** * param text 待解密原文* param crack 盐值密钥* return 解密后的字符串* Description: jasypt 解密PBEWITHHMACSHA512ANDAES_256*/public static String decryptWithSHA512(String text, String crack) {
//1.创建加解密工具实例PooledPBEStringEncryptor encryptor new PooledPBEStringEncryptor();
//2.加解密配置SimpleStringPBEConfig config new SimpleStringPBEConfig();config.setPassword(crack);config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
// 为减少配置文件的书写以下都是 Jasypt 3.x 版本配置文件默认配置config.setKeyObtentionIterations(1000);config.setPoolSize(1);config.setProviderName(SunJCE);config.setSaltGeneratorClassName(org.jasypt.salt.RandomSaltGenerator);config.setIvGeneratorClassName(org.jasypt.iv.RandomIvGenerator);config.setStringOutputType(base64);encryptor.setConfig(config);
//3.解密return encryptor.decrypt(text);}
}