springboot对数据库密码加密
第一步:maven引jar包
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>1.14</version> </dependency> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot</artifactId> <version>1.14</version> </dependency>
第二步:配置文件
#jasypt加密的盐值
jasypt.encryptor.password=erp
第三步:调用加密方法
public class JasyptTest { @Autowired static StringEncryptor encryptor; //加密 public static void getPass(){ encryptor = SpringUtil.getBean(StringEncryptor.class); String name = encryptor.encrypt("root"); String password = encryptor.encrypt("firewall_new"); System.out.println(name); //hifVDwDPW5MNYa0tAabvJQ== System.out.println(password); //5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi } //解密 public static void passDecrypt(){ encryptor = SpringUtil.getBean(StringEncryptor.class); String username = encryptor.decrypt("hifVDwDPW5MNYa0tAabvJQ=="); String password = encryptor.decrypt("5mTXmB5IYPxqiSuD/045cjHEkJz2NhNi"); System.out.println(username+"--"+password); } }
package com.interact.firewall.common; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.core.env.Environment; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; import java.io.File; import java.io.IOException; import java.util.List; @Component public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; private static Logger logger = LoggerFactory.getLogger(SpringUtil.class); @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtil.applicationContext == null){ SpringUtil.applicationContext = applicationContext; } } public static void setContext(ApplicationContext applicationContext){ if(applicationContext != null){ SpringUtil.applicationContext = applicationContext; } } public static ApplicationContext getApplicationContext(){ return applicationContext; } public static <T> T getBean(Class<T> clazz){ try{ return getApplicationContext().getBean(clazz); }catch (Throwable e){ logger.error("get bean error",e); } return null; } public static String getProperty(String key,String defaultVal){ try{ return SpringUtil.getApplicationContext().getEnvironment().getProperty(key,defaultVal); }catch (Throwable e){ logger.error("获取pro失败:",e); return defaultVal; } } public static String getProperty(String key){ return getProperty(key,""); } public static String[] getActiveProFiles() throws IOException { return getApplicationContext().getEnvironment().getActiveProfiles(); } private static void writeProperty(String proName,String value){ try{ if(StringUtils.isBlank(proName)){ return; } String[] activeProFiles = getActiveProFiles(); for (String active : activeProFiles){ Resource resource = applicationContext.getResource("classpath:/application-"+active+".properties"); if(!resource.exists()){ String configPath = SpringUtil.getProperty("spring.profiles.path", ""); resource = applicationContext.getResource("file:"+configPath+"application-"+active+".properties"); } File file = resource.getFile(); List<String> lines = FileUtils.readLines(file); boolean find = false; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if(StringUtils.startsWith(line,proName)){ line = proName + "="+value; lines.set(i,line); find = true; } } if( !find ){ lines.add(proName+"="+value); } FileUtils.writeLines(file,lines); ContextRefresher bean = getBean(ContextRefresher.class); bean.refresh(); } }catch (Exception e){ e.printStackTrace(); } } }
控制台就会输出加密后的秘钥。
切记:将上面的生成的密匙如下替换,此处主要是数据库密码密文使用ENC进行标识