• 如何保护SpringBoot配置文件中的敏感信息


    如何保护SpringBoot配置文件中的敏感信息

    使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些。

    打开application.propertiesapplication.yml,比如 MySql登陆密码,Redis登陆密码以及第三方的密钥

    等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。

    jasypt由一个国外大神写了一个springboot下的工具包,用来加密配置文件中的信息。

    GitHub Demo地址:

    https://github.com/jeikerxiao/spring-boot2/tree/master/spring-boot-encrypt

    下面以数据库用户名和数据库密码加密为例进行介绍。

    1、pom依赖

    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0modelVersion>
        <parent>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-parentartifactId>
            <version>2.5.6version>
            <relativePath/>
        parent>
    
        <groupId>com.jasyptgroupId>
        <artifactId>spring-boot-jasyptartifactId>
        <version>0.0.1-SNAPSHOTversion>
        <name>spring-boot-jasyptname>
        <description>如何保护 SpringBoot 配置文件中的敏感信息description>
    
        <properties>
            <java.version>1.8java.version>
        properties>
    
        <dependencies>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>2.4.5version>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
            dependency>
    
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-testartifactId>
                <scope>testscope>
            dependency>
    
            
            <dependency>
                <groupId>com.github.ulisesbocchiogroupId>
                <artifactId>jasypt-spring-boot-starterartifactId>
                <version>2.1.0version>
            dependency>
    
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>RELEASEversion>
            dependency>
    
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
            dependency>
    
        dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    查看最新版本可以到:

    https://github.com/ulisesbocchio/jasypt-spring-boot

    2、配置加/解的密码

    # jasypt加密的密匙
    jasypt:
      encryptor:
        password: Y6M9fAJQdU7jNp5MW
    
    • 1
    • 2
    • 3
    • 4

    3、测试用例中生成加密后的秘钥

    package com.jasypt;
    
    import org.jasypt.encryption.StringEncryptor;
    import org.junit.Assert;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    class SpringBootJasyptApplicationTests {
    
        @Autowired
        private StringEncryptor encryptor;
    
        @Test
        public void getPass() {
            String url = encryptor.encrypt("jdbc:mysql://127.0.0.1:3306/test");
            String name = encryptor.encrypt("root");
            String password = encryptor.encrypt("root");
            System.out.println("database url: " + url);
            System.out.println("database name: " + name);
            System.out.println("database password: " + password);
            Assert.assertTrue(url.length() > 0);
            Assert.assertTrue(name.length() > 0);
            Assert.assertTrue(password.length() > 0);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    下面是输出加密字符串:

    database url: CCGZpukXHOy7xPMVm6//IZi3kQxJQmZuFdrje1KtshlZD0IiwrHTQWcB4J4l1qKe
    database name: +DcWhoH0RuZck93R2FSEEg==
    database password: gTVoDqbBeaTe2+omIsoXIA==
    
    • 1
    • 2
    • 3

    4、将加密后的字符串替换原明文

    server:
      port: 8080
    spring:
      # 数据库相关配置
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: CCGZpukXHOy7xPMVm6//IZi3kQxJQmZuFdrje1KtshlZD0IiwrHTQWcB4J4l1qKe
        username: +DcWhoH0RuZck93R2FSEEg==
        password: gTVoDqbBeaTe2+omIsoXIA==
      jpa:
        hibernate:
          ddl-auto: update
        show-sql: true
      # 返回的api接口的配置,全局有效
      jackson:
       # 如果某一个字段为null,就不再返回这个字段
        default-property-inclusion: non_null
        date-format: yyyy-MM-dd HH:mm:ss
        serialization:
          write-dates-as-timestamps: false
        time-zone: GMT+8
    # jasypt加密的密匙
    jasypt:
      encryptor:
        password: Y6M9fAJQdU7jNp5MW
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    5、部署时配置salt(盐)值

    为了防止salt(盐)泄露,反解出密码,可以在项目部署的时候使用命令传入salt(盐)值:

    $ java -jar xxx.jar -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW
    
    • 1

    或者在服务器的环境变量里配置,进一步提高安全性。

    打开/etc/profile文件

    $ vim /etc/profile
    
    • 1

    在profile文件末尾插入salt(盐)变量

    $ export JASYPT_PASSWORD = Y6M9fAJQdU7jNp5MW
    
    • 1

    编译,使配置文件生效

    $ source /etc/profile
    
    • 1

    运行

    $ java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar
    
    • 1
  • 相关阅读:
    洛谷P2523 Problem c
    Python3 语法简明教程
    【博主已解决】【npm】npm publish 发布包报错 代码:ERR400
    Spring底层原理学习笔记--第十讲--(aop之agent增强)
    几种常见的垃圾回收器和垃圾回收算法
    Linux 文件、目录和用户权限管理指南
    JVM 垃圾回收器分类
    二十二、W5100S/W5500+RP2040树莓派Pico<SMTP发送邮件>
    JZ31 栈的压入、弹出序列
    科技云报道:云计算走向工业互联网“深水区”
  • 原文地址:https://blog.csdn.net/qq_30614345/article/details/131969201