iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >如何给yml配置文件的密码加密(SpringBoot)
  • 130
分享到

如何给yml配置文件的密码加密(SpringBoot)

yml配置文件yml密码加密yml配置文件密码加密 2022-11-13 18:11:10 130人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录1.低版本2.x1)引入jar包2)生成密码3)测下解密4)yml配置5)测测登录2.高版本 3.x1)引入jar包2)生成密码3)yml配置最近在忙着解决规约扫描的问题,其一就

最近在忙着解决规约扫描的问题,其一就是这个明文密码必须加密的问题,一般是数据库的配置。首先我用的是默认的PBEWithMD5AndDES默认的MD5加密方式,

弄好之后有要求使用AES_256/SM2/SM4等高级的算法加密,于是后来又升级了jar包使用默认的PBEWITHHMacSHA512ANDAES_256.

jdk版本-1.8

1.低版本2.x

先记录低版本的加密方式-----PBEWithMD5AndDES

1)引入jar包

<dependency>
            <groupId>com.GitHub.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>

如上一个启动类,在联网的情况下就能自动下载其余加密需要的jar包。

如果是在内网的情况下,可以手动在项目的lib目录引入所需jar包,如下:

<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot</artifactId>
    <version>2.1.0</version>
</dependency>

2)生成密码

@Test
    public void testEncrypt() {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlGorithm("PBEWithMD5AndDES");          // 加密的算法,这个算法是默认的
        config.setPassword("Angel");                        // 加密的密钥,随便自己填写,很重要千万不要告诉别人
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "123456";         //自己的密码
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptedText);
    }

如图,数据库密码为123456,密钥为Angel,加密算法也如上;

运行如图:4o+eS8OaWQ7HcjVgrkoX0A==

3)测下解密

@Test
    public void testDe() {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
 
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("Angel");
        standardPBEStringEncryptor.setConfig(config);
        String encryptedText = "4o+eS8OaWQ7HcjVgrkoX0A==";   //加密后的密码
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println(plainText);
    }

运行如图:

4)yml配置

记好加密好的密码,在yml文件里将数据源那里用ENC套上,如下:

spring:
 datasource:
  username: root
  passWord: ENC(4o+eS8OaWQ7HcjVgrkoX0A==)
  url: jdbc:Mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver
 
jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWithMD5AndDES

5)测测登录

题外话,启动类上无需开启加密注解(@EnableEncryptableProperties)

2.高版本 3.x

1)引入jar包

<dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.3</version>
        </dependency>

与前面 一致,没有外网的条件,引入如下jar包

<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt -->
<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.3</version>
</dependency>
<!-- Https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot</artifactId>
    <version>3.0.3</version>
</dependency>

2)生成密码

这里用的是一个工具类,CV大法。

package com.example.demo.utils;
 

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
 
public class JasypUtil {
 
    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
 
    
    public static String encryptWithSHA512(String plainText, String factor) {
        // 1. 创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
        config.seTKEyObtentionIterations( "1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 3. 加密
        return encryptor.encrypt(plainText);
    }
 
    
    public static String decryptWithSHA512(String encryptedText, String factor) {
        // 1. 创建加解密工具实例
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 2. 加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(factor);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        // 为减少配置文件的书写,以下都是 Jasyp 3.x 版本,配置文件默认配置
        config.setKeyObtentionIterations( "1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        // 3. 解密
        return encryptor.decrypt(encryptedText);
    }
    
    public static void main(String[] args) {
        String factor = "Angel";
        String plainText = "123456";
 
        String encryptWithSHA512Str = encryptWithSHA512(plainText, factor);
        String decryptWithSHA512Str = decryptWithSHA512(encryptWithSHA512Str, factor);
        System.out.println("采用AES256加密前原文密文:" + encryptWithSHA512Str);
        System.out.println("采用AES256解密后密文原文:" + decryptWithSHA512Str);
    }
}

执行这个main方法,来,见证奇迹的时刻

Exception in thread "main" org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.handleInvalidKeyException(StandardPBEByteEncryptor.java:1207)
    at org.jasypt.encryption.pbe.StandardPBEByteEncryptor.encrypt(StandardPBEByteEncryptor.java:996)
    at org.jasypt.encryption.pbe.StandardPBEStringEncryptor.encrypt(StandardPBEStringEncryptor.java:655)
    at org.jasypt.encryption.pbe.PooledPBEStringEncryptor.encrypt(PooledPBEStringEncryptor.java:465)
    at com.example.demo.utils.JasypUtil.encryptWithSHA512(JasypUtil.java:41)
    at com.example.demo.utils.JasypUtil.main(JasypUtil.java:78)

这个错,很有意思,在加密的时候,手动配置了JCE的方法来支持加密的程序,然而jasypt3.x版本的对于jdk8自带的jce的包不兼容,需要升级一下,所以到网上下载jdk8对应的JCE包jce_policy-8。

下载后解压有两个jar包,如图:

到该目录下替换即可:C:\Program Files\Java\jdk1.8.0_25\jre\lib\security

 然后运行main方法:8jLUdq0Fr7UhJGNwK/Nc6i6/WV4+UBpvtfBLDh4e3jZMJZAhPqfZdGlpFEUk24UZ

成功。

3)yml配置

spring:
 datasource:
  username: root
  password: ENC(8jLUdq0Fr7UhJGNwK/Nc6i6/WV4+UBpvtfBLDh4e3jZMJZAhPqfZdGlpFEUk24UZ)
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
  driver-class-name: com.mysql.cj.jdbc.Driver
 
jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWITHHMACSHA512ANDAES_256

 改个登录密码:

很顺利,不妨再来验证加密算法,如图我如果改为

jasypt:
 encryptor:
  password: Angel
  algorithm: PBEWithMD5AndDES

运行项目则报错,图略。

yml文件里面需要配置密钥以及算法,才能正确解密访问数据库,那你看这个Angel密钥是用password来修饰的,是否会被外行的人认作密码呢?

答案是肯定的,所以呀需要使用写小手段,把密钥也写成ENC()这种格式的就OK了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 如何给yml配置文件的密码加密(SpringBoot)

本文链接: https://www.lsjlt.com/news/170290.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作