网站高防服务器租用,制作二维码的微信小程序,中铁三局招聘官网,泸州市网站建设场景
经常会遇到这样一种情况#xff1a;项目的配置文件中总有一些敏感信息#xff0c;比如数据源的url、用户名、
密码....这些信息一旦被暴露那么整个数据库都将会被泄漏#xff0c;那么如何将这些配置隐藏呢。
除了使用手动将加密之后的配置写入到配置文件中#xff…场景
经常会遇到这样一种情况项目的配置文件中总有一些敏感信息比如数据源的url、用户名、
密码....这些信息一旦被暴露那么整个数据库都将会被泄漏那么如何将这些配置隐藏呢。
除了使用手动将加密之后的配置写入到配置文件中提取的时候再手动解密的方式还可以使用如下
方式。
jasypt-spring-boot
GitHub - ulisesbocchio/jasypt-spring-boot: Jasypt integration for Spring boot
Jasypt-Spring-Boot: Jasypt Spring Boot 为 Spring Boot 项目中的属性源提供加密支持
注
博客霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主
实现
1、SpringBoot中添加项目依赖 dependencygroupIdcom.github.ulisesbocchio/groupIdartifactIdjasypt-spring-boot-starter/artifactIdversion3.0.3/version/dependency
2、在配置文件yml中添加一个加密时的秘钥可任意指定
jasypt:encryptor:password: badaodechengxvyuan
直接将秘钥放在配置文件中也是不安全可以在项目启动的时候配置秘钥
java -jar xxx.jar -Djasypt.encryptor.passwordbadaodechengxvyuan
3、编写单元测试生成加密后的数据
为了将配置的明文数据进行加密需要将数据进行加密
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.Assert;SpringBootTest
class JasyptTest {/*** inject encryption method*/Autowiredprivate StringEncryptor encryptor;/*** encrypt data*/Testvoid encrypt() {String name encryptor.encrypt(root);String password encryptor.encrypt(123456);System.out.println(name:name);System.out.println(password:password);Assert.isTrue(name.length()0,name encrypt success);Assert.isTrue(password.length()0,password encrypt success);}
}
这里为了演示只加密了用户名和密码当然url或者其他需要加密的明文数据都可以。
运行单元测试会输出加密后的字符串数据 4、将加密后的数据复制到配置文件yml中对应的位置并使用ENC()包裹。
# 数据源
spring:application:name: badao-tcp-demodatasource:url:jdbc:mysql://localhost:3306/test?useUnicodetruecharacterEncodingutf8zeroDateTimeBehaviorconvertToNulluseSSLtrueserverTimezoneGMT%2B8username: ENC(Ls/gHnNIUDGsGIRbkKuKaa2E...)password: ENC(rCRmLz/Iiu4INB/3YKVGxC...) 上面包裹的前缀和后缀也可通过配置进行更改以及更多用法比如自己配置加密算法
可参考官方文档。