SpringBoot项目配置文件中密码加密

1、引入依赖

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

2、yml配置文件中增加配置:

jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES    #加密算法
    password: my-secret-key        #加密秘钥,可自己随意配置的字符串

3、在springboot测试类中传入密码获取加密后的密文

//注入对象
@Resource
private StringEncryptor stringEncryptor;

//转换并输出密码为密文
String ciphertext = stringEncryptor.encrypt("root");
System.out.println(ciphertext);

4、将配置文件中密码进行修改,括号中为上一步控制台输出的密文

password: ENC(OnOQo1JAZrlJJq5GcamNWL/XMRin6mPGt8LNudWNBL8=)

注:如有springcloud环境可不采用上述方式,利用springcloud config server提供的加密功能。

posted @ 2025-08-28 16:43  Leil_blogs  阅读(20)  评论(0)    收藏  举报