栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

学习笔记001——SpringBoot的yml文件中密码加密

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习笔记001——SpringBoot的yml文件中密码加密

1、pom文件导入依赖



    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    2.1.0

2、对密码加密

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("htl");
        standardPBEStringEncryptor.setConfig(config);
        //密码(未加密)
        String plainText = "123456";
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptedText);
    }

    //解密
    @Test
    public void testDe() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("htl");
        standardPBEStringEncryptor.setConfig(config);
        //加密后的密码
        String encryptedText = "UXavnIi3he8znD2ZJk/4yQ==";
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println(plainText);
    }
}

3、yml文件添加:

jasypt:
  encryptor:
    # 你的秘钥
    password: htl

4、加密后的密码要放到  ENC()  里面

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/001project
    username: root
    password: ENC(GNIfWMjZMce08rqqnPoOZBx8T3yVNd81)

5、启动类添加 @EnableEncryptableProperties 注解

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication()
@MapperScan("com.yunshe.mapper")
@EnableEncryptableProperties //开启加密注解
public class ProjectApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(ProjectApplication.class, args);
    }

}

PS:发博客,仅仅是当作学习的笔记~

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988056.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号