JAVA进阶

springboot配置数据库密码加密

2022-03-29  本文已影响0人  driver_ab
  1. 导入依赖
<!-- 加密依赖 -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>
  1. 加密
package cn.linjk.ehome;
  
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.Test;
  
public class JasyptTest {
 @Test
 public void testEncrypt() throws Exception {
 StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
 EnvironmentPBEConfig config = new EnvironmentPBEConfig();
  
 config.setAlgorithm("PBEWithMD5AndDES");  // 加密的算法,这个算法是默认的
 config.setPassword("12345");   // 加密的密钥
 standardPBEStringEncryptor.setConfig(config);
 String plainText = "hello";
 String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
 System.out.println(encryptedText);
 }
}
  1. 在application.yml文件中配置
//之前的盐
jasypt:
  encryptor:
    password: 12345
    algorithm: PBEWithMD5AndDES
    ivGeneratorClassname: org.jasypt.salt.RandomIVGenerator

//ENC(加密的密文)
spring:
  datasource:
    username:ENC(sBdk7fXxdnCZMzPjGkZr0g==)
    password:ENC(sBdk7fXxdnCZMzPjGkZr0g==)
  1. 启动类加入注解
/配置数据库加密注解
@EnableEncryptableProperties
上一篇 下一篇

猜你喜欢

热点阅读