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

方式一

引入第三方jar,快速加密

1、引入依赖

<dependency>
    <groupId>com.github.ulisesbocchiogroupId>
    <artifactId>jasypt-spring-boot-starterartifactId>
    <version>2.1.0version>
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

你可能感兴趣的:(springboot项目相关,spring,boot,后端,java)