springboot项目给配置文件yml或者properties中的密码加密

方式一

引入第三方jar,快速加密

1、引入依赖

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

2、加密密码生成

2-1、先配置加密盐

# 加密方式配置
jasypt:
  encryptor:
    password: toolcenter #加密盐,可自定义,这里不是写数据库密码
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

配置完加密盐再生成下面的密码


import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

public class SystemApplication {
    @Autowired
    private static StringEncryptor stringEncryptor;
    public static void main(String[] args) {
        String String enpassword=stringEncryptor.encrypt("qQq314159@26");//密码1
        String enpassword1=stringEncryptor.encrypt("root");//密码2
        System.out.println("encode password:" + enpassword);
        System.out.println("encode password1:" + enpassword1);
        System.out.println("decode password:" + stringEncryptor.decrypt(enpassword));
    }

}


//输出
encode password:PesgSGv2K77NxzcJtqqUN9OAqDvu790l
encode password1:7zOMC++Gyf1HR3iqndLPWA==
decode password:qQq314159@26

3、配置文件修改为加密后的密码

# 加密方式配置
jasypt:
  encryptor:
    password: toolcenter #加密盐,可自定义,这里不是写数据库密码
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator
#配置密码加密
spring:
  application:
    name: iotdata-system
  datasource:
    url: jdbc:postgresql://localhost:5432/tool_center
    username: postgres
    password: ENC(PesgSGv2K77NxzcJtqqUN9OAqDvu790l)
    driver-class-name: org.postgresql.Driver
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值