Spring Boot给application.yml里面的数据库基础信息进行简单密文处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36827957/article/details/82256481

采用的应该是基于MD5的对称加密,通过密钥得到密文,下面是具体步骤:

第一步:首先加入相应的jar包

<!-- 数据库加密 -->
		<dependency>
			<groupId>com.github.ulisesbocchio</groupId>
			<artifactId>jasypt-spring-boot-starter</artifactId>
			<version>1.16</version>
		</dependency>

第二步需要编写一个测试类给需要加密的字段进行加密,得到密文

package com.springboot.demo;

import com.leaveword.controller.UserController;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 633805 LYH
 * @version V1.0
 * @description 对类的描述
 * @update 2018/8/31
 * @since 1.7
 */
public class Test {

    @org.junit.Test
    public void testEncrypt() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");          // 加密的算法,这个算法是默认的
        config.setPassword("lyh");                        // 加密的密钥
        standardPBEStringEncryptor.setConfig(config);
        //加密用户信息
        String plainText = "用户名";
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        //加密密码信息
        String Enpassword = "密码";
        String EnpasswordText = standardPBEStringEncryptor.encrypt(Enpassword);
        //加密地址信息
        String DBAUrl = "数据库地址信息";
        String DBAUrlText = standardPBEStringEncryptor.encrypt(DBAUrl);
        System.out.println("用户"+encryptedText);
        System.out.println("密码"+EnpasswordText);
        System.out.println("地址"+DBAUrlText);
    }
    @org.junit.Test
    public void testDe() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("lyh");
        standardPBEStringEncryptor.setConfig(config);
        String encryptedText = "jQP16AVgmh990wPJeeoYmw==";
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println(plainText);
    }


}

第三部 填写application.yml配置

首先要把密钥填入,用来进行解密

jasypt:
  encryptor:
    password: lyh

接下来把刚刚获取的密文填入配置文件中,通过ENC("")来进行解密

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: ENC(ylHvLkRYjPrj9YXpu4miMGaH9sgTpB5POShHmsRCFQDMVPXA1wvCKEMWkRXqDnHWSdnHQ0MGJfKqJzWnRb+qE2glRHC60a6rijfXurhXSDD2O8gaUspGsw==) 
    username: ENC(jQP16AVgmh990wPJeeoYmw==)   
    password: ENC(u+DcAA3L7DTm2yUhG+YPrw==) 

到此为止加密就完成了。可以进行测试了

猜你喜欢

转载自blog.csdn.net/qq_36827957/article/details/82256481
今日推荐