当前位置: 代码网 > it编程>编程语言>Java > SpringBoot集成ENC对配置文件进行加密的流程步骤

SpringBoot集成ENC对配置文件进行加密的流程步骤

2024年12月19日 Java 我要评论
配置文件加密,集成enc引入pom依赖 <!-- enc配置文件加密 --> <dependency> <groupid>com.github.

配置文件加密,集成enc

  • 引入pom依赖

 <!-- enc配置文件加密 -->
    <dependency>
        <groupid>com.github.ulisesbocchio</groupid>
        <artifactid>jasypt-spring-boot-starter</artifactid>
        <version>2.1.2</version>
    </dependency>
  1. 【注意】开始使用的3.1.5版本,启动报错:reason: org.springframework.boot.context.properties.bind.bindexception: failed to bind properties under 'spring.redis.password' to java.lang.string

    报错原因:因为3.x版本的默认加密算法变了,所以对应配置文件加密算法配置得修改,同时加密的方法也得用新的加密算法计算出加密字符串。

  2. 使用工具类对明文数据进行加密处理,使用完后为了安全起见,可以删除加密盐值

package com.basic.test;

import org.jasypt.util.text.basictextencryptor;
import org.junit.test;

/**
 * classname: crypttest
 * package: com.basic.test
 * description:
 *
 * @author 王涵
 * @date 2024/12/17 14:18
 * @version 1.0
 */
public class crypttest {

    /**
     * 加密盐值,使用完成后进行删除,或者不能提交到`生产环境`,比如:
     */
    private final static string password = "basic";

    @test
    public void enctest() {
        basictextencryptor encryptor = new basictextencryptor();
        // 用于设置加密密钥。密钥是用于加密和解密字符串的关键信息。
        encryptor.setpassword(password);

        // 明文1
        string name_encrypt = "wwwbaiducom";
        // 明文2
        string password_encrypt = "123456";

        // 明文加密
        string encrypt1 = encryptor.encrypt(name_encrypt);
        string encrypt2 = encryptor.encrypt(password_encrypt);
        system.out.println("明文加密1:" + encrypt1);
        system.out.println("明文加密2:" + encrypt2);

        // 密文解密
        string decrypt1 = encryptor.decrypt(encrypt1);
        string decrypt2 = encryptor.decrypt(encrypt2);
        system.out.println("密文解密1:" + decrypt1);
        system.out.println("密文解密2:" + decrypt2);
    }
}
  • 将加密后的密码配置到配置文件,使用enc()包裹。不需要enc时可以直接写密码,不用enc包裹即可

 # 主库数据源
 master:
     url: jdbc:mysql://127.0.0.1:3306/test?useunicode=true&characterencoding=utf8&zerodatetimebehavior=converttonull&usessl=true&servertimezone=gmt%2b8
     username: root
     password: enc(dbofmyydjokjykt1dx/jmzpbcipbhhep)

运行时配置

  • 打开run/debug configuratios

  • 打开modify options选择add vm optiosn

  • 配置vm options为自己的加密盐值

启动类配置@enableencryptableproperties

package com.basic;

import com.ulisesbocchio.jasyptspringboot.annotation.enableencryptableproperties;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration;

/**
 * @author wanghan
 * 心中那自由的世界,如此的清澈高远。
 * @date 2024/12/6 14:47
 * @description 启动程序
 */
@springbootapplication(exclude = { datasourceautoconfiguration.class })
@enableencryptableproperties
public class basicapplication
{
    public static void main(string[] args)
    {
        // 热部署生效 true 开启热部署 false 关闭热部署
        system.setproperty("spring.devtools.restart.enabled", "false");
        springapplication.run(basicapplication.class, args);
        system.out.println("(♥◠‿◠)ノ゙  基础框架启动成功   ლ(´ڡ`ლ)゙");
    }
}

jar包运行指定加密规则

cd /opt/home/test/
nohup java -xms512m -xmx512m -jar -djasypt.encryptor.password=0tjtdbl6lwk73gclvnd2gc1rdbfgcx test-base.jar --spring.profiles.active=dev > test-base.out 2>&1 &
echo start test-base		

部署到tomcat

export java_opts="-djasypt.encryptor.password=0tjtdbl6lwk73gclvnd2gc1rdbfgcx"

以上就是springboot集成enc对配置文件进行加密的流程步骤的详细内容,更多关于springboot enc配置文件加密的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com