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

admin 2021年9月22日21:20:00评论54 views字数 3076阅读10分15秒阅读模式

点击下方“IT牧场”,选择“设为星标”


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

来源:blog.csdn.net/jeikerxiao/article/details/96480136

说明

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

打开application.propertiesapplication.yml,比如 MySql登陆密码,Redis登陆密码以及第三方的密钥等等一览无余,这里介绍一个加解密组件,提高一些属性配置的安全性。

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

GitHub Demo地址

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

数据用户名和数据库密码加密为例

1. 引入包

查看最新版本可以到:

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

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

2. 配置加/解的密码

# jasypt加密的密匙
jasypt:
  encryptor:
    password: Y6M9fAJQdU7jNp5MW

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

@RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseTest 
{

    @Autowired
    private StringEncryptor encryptor;

    @Test
    public void getPass() {
        String url = encryptor.encrypt("jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
        String name = encryptor.encrypt("root");
        String password = encryptor.encrypt("123456");
        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);
    }
}

下面是输出加密字符串:

database url: 6Ut7iADnHS18cManoFJuNRQ5QEDfcho/F96SOhsHZdXlHYCa5PSrz6rk48I9eHB7qPp5AxDFBk9xi0I1hi6BJ0DSPYA9443gBAk5JDUxDufjUKsdh6knZJLNELmFJzYrDvCu4S0x22MYdZqJDLbyDUU2JcoezCvs156vmsPgU4A=
database name: fmai72yGYKGlP6vTtX77EQ==
database password: GPMG7FGV+EA9iGkC27u67A==

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

applicatioin.yml

server:
  port: 8080
spring:
  # 数据库相关配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 这里加上后缀用来防止mysql乱码,serverTimezone=GMT%2b8设置时区
    url: ENC(h20YiPrvNnuuTGjlrE1RVpudMuIQAS6ZPSVo1SPiYVyLen7/TWI5rXVRkStA3MDcoVHQCmLa70wYU6Qo8wwtnsmaXa5jykD3MNhAp5SGJxHsTG5u7tflPdnNmOufyhdsYPxBGWAgibYs9R7yBfrvtwBTRbe096APd3bnG3++Yro=)
    username: ENC(sT6BztXbJEa71eg3pPGYMQ==)
    password: ENC(MpSZFJ9ftq+3+VUANZjr0Q==)
  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

注意: 上面的 ENC() 是固定写法.

附言

部署时配置salt(盐)值

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

java -jar xxx.jar  -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW

或者在服务器的环境变量里配置,进一步提高安全性。学习资料:Java进阶视频资源

打开/etc/profile文件

vim /etc/profile

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

export JASYPT_PASSWORD = Y6M9fAJQdU7jNp5MW

编译,使配置文件生效

source /etc/profile

运行

java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar

干货分享

最近将个人学习笔记整理成册,使用PDF分享。关注我,回复如下代码,即可获得百度盘地址,无套路领取!

001:《Java并发与高并发解决方案》学习笔记;002:《深入JVM内核——原理、诊断与优化》学习笔记;003:《Java面试宝典》004:《Docker开源书》005:《Kubernetes开源书》006:《DDD速成(领域驱动设计速成)》007:全部008:加技术群讨论

加个关注不迷路

喜欢就点个"在看"呗^_^

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2021年9月22日21:20:00
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   如何保护 Spring Boot 配置文件中的敏感信息http://cn-sec.com/archives/553776.html

发表评论

匿名网友 填写信息