当前位置: 代码网 > it编程>编程语言>Java > 对SpringBoot项目配置文件进行加密实现过程

对SpringBoot项目配置文件进行加密实现过程

2025年10月24日 Java 我要评论
1.springboot项目pom添加maven依赖<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt

1.springboot项目pom添加maven依赖

<!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
<dependency>
    <groupid>com.github.ulisesbocchio</groupid>
    <artifactid>jasypt-spring-boot-starter</artifactid>
    <version>3.0.5</version>
</dependency>

2.使用单元测试生成加密数据

设置配置文件(bootstrap.yml或者application.yml)

jasypt:
  encryptor:
    password: jasypt!di@soc
    algorithm: pbewithmd5anddes
  • password:加密的盐
  • algorithm:加密算法,这里使用 pbewithmd5anddes

运行单元测试文件

package com.pscsoft.code.isoc;

import org.jasypt.encryption.stringencryptor;
import org.junit.jupiter.api.test;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.boot.test.context.springboottest;
import org.springframework.context.annotation.import;

/**
 * @author avry.jiang
 * @date 2023-06-28 17:45:58
 */
@springboottest()
@import({isocapplication.class})
public class jasyptutiltest {

    @autowired
    private stringencryptor encryptor;

    @test
    public void jasypt() {
        string name = encryptor.encrypt("12345678");
        system.out.println("en: " + name);
        system.out.println("de: " + encryptor.decrypt(name));
    }

}

执行结果:

3.在springboot项目中使用

配置mysql的jdbc,使用“enc(密文)”配置密码,其他配置有密码都类似修改

4.将加密盐放入配置文件、启动环境或者启动命令中

4.1.idea启动项配置

--jasypt.encryptor.password=jasypt!di@soc --jasypt.encryptor.algorithm=pbewithmd5anddes

4.2.配置文件(不推荐,会暴露加密盐反推明文的)

jasypt:
  encryptor:
    password: jasypt!di@soc
    algorithm: pbewithmd5anddes

4.3.其他

如果是docker启动,可以配置在dockerfile中指定启动命令

entrypoint ["java","-jar","-xms1024m", "-xmx1024m","--jasypt.encryptor.password=jasypt!di@soc","--jasypt.encryptor.algorithm=pbewithmd5anddes", "/xxx/xxx.jar"]

启动项目即可~

总结

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

(0)

相关文章:

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

发表评论

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