美文网首页JAVA进阶
springboot配置数据库密码加密

springboot配置数据库密码加密

作者: driver_ab | 来源:发表于2022-03-29 18:20 被阅读0次
    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
    

    相关文章

      网友评论

        本文标题:springboot配置数据库密码加密

        本文链接:https://www.haomeiwen.com/subject/iyhbjrtx.html