前言:为了使我们的数据库url脱敏,我们可以对数据库连接的url和用户名及密码进行加密操作,这边我们将使用 jasypt-spring-boot 进行加密。
一、Maven 引入jar
1. 如果我们项目中使用了@SpringBootApplication 或者@EnableAutoConfiguration。此为全局启用可加密属性(这边是最新的jar)
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
2. 如果您不使用@SpringBootApplication
或@EnableAutoConfiguration
自动配置注释,则将此依赖项添加到您的项目中:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.4</version>
</dependency>
然后添加@EnableEncryptableProperties
到您的配置类。例如:
@Configuration
@EnableEncryptableProperties
public class MyApplication {
...
}
并且将在整个 Spring 环境中启用可加密属性
3. 如果您不使用 @SpringBootApplication
或 @EnableAutoConfiguration
自动配置注释,并且您不想在整个 Spring 环境中启用可加密属性,那么还有第三种选择。首先将以下依赖项添加到您的项目中
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.4</version>
</dependency>
然后@EncryptablePropertySource在配置文件中添加任意数量的注释。就像您使用 Spring 的@PropertySource注释一样。例如:
@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
...
}
@EncryptablePropertySources 可以进行分组例如:
@Configuration
@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
@EncryptablePropertySource("classpath:encrypted2.properties")})
public class MyApplication {
...
}
二、进行加密操作,写一个main方法
public static void main(String[] args) {
StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
/*配置文件中配置如下的算法*/
standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
/*加密盐自定义*/
standardPBEStringEncryptor.setPassword("chendazui@123456");
/*要加密的文本*/
String url = standardPBEStringEncryptor.encrypt("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
String name = standardPBEStringEncryptor.encrypt("root");
String password =standardPBEStringEncryptor.encrypt("chendazui");
/*将加密的文本写到配置文件中*/
System.out.println("url="+url);
System.out.println("name="+name);
System.out.println("password="+password);
}
加密结果如下:
三、将原先的url、账号名、密码进行替换
加密结果需要将ENC(*)包含加密值,Spring加载时会自动解析
jasypt:
encryptor:
# 加密盐值 必须和生成的盐值一样
password: chendazui@123456
# 加密算法设置 3.0.0 以后需要加上下面两个配置
algorithm: PBEWithMD5AndDES
iv-generator-classname: org.jasypt.iv.NoIvGenerator
spring:
profiles:
active: local
application:
name: demo
datasource:
url: ENC(K2uhdMjfo5n8OXBVY9wH30Do7BH40wb4bQ9murC+dzXtJrn0p45xHoHcIMtUTofUuQB4Kf0BrNoaxYHrLaNa3nneM6S1tIUFnTqCoo8pOioeoCTV2CkFdYds7ZNI43jP/SGaDL4RwPyiAtRQCCAPwNI1N6nyBKsHJuoi3GmBec/v/6VwRf3uMLKvbR2pi74G9IAhB0H7G+8=)
username: ENC(ZsKmSXIhxscqFYHwPpeQ2g==)
password: ENC(DxvmmL9lRu1hVhq7S9PbWkgInq/9viMS)
driver-class-name: com.mysql.cj.jdbc.Driver
四、加密结果与加密盐值隔离
更改jvm虚拟机配置信息
JAVA_OPTS="-Djasypt.encryptor.password=chendazui@123456 -Djasypt.encryptor.algorithm=PBEWithMD5AndDES -Djasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator"
版权声明:本文为qq_40438883原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。