IT评测·应用市场-qidao123.com技术社区
标题:
SpringBoot安全升级指南:运用ENC加密强化配置文件防护
[打印本页]
作者:
天津储鑫盛钢材现货供应商
时间:
2025-4-1 04:31
标题:
SpringBoot安全升级指南:运用ENC加密强化配置文件防护
使用 Jasypt 加密 Spring Boot 配置中的敏感信息
在开辟应用步伐时,保护敏感信息如数据库密码、API 密钥等黑白常重要的。Spring Boot 提供了多种方式来处理配置文件中的敏感数据,其中一种有效的方法是使用 Jasypt(Java Simplified Encryption)。本文将先容怎样使用 Jasypt 来加密和解密 Spring Boot 应用步伐中的敏感信息,并提供一个简朴的命令行工具用于天生加密后的值。
1. 添加依靠
起首,在你的 pom.xml 文件中添加必要的依靠项:
<dependencies>
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.4</version>
</dependency>
<!-- 其他依赖 -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
复制代码
确保你选择了与你的 Spring Boot 版本兼容的 Jasypt 版本。
2. 创建自界说属性解析器
创建一个类 CustomEncryptablePropertyResolver 来实现属性值的解密逻辑:
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class CustomEncryptablePropertyResolver implements EncryptablePropertyResolver {
private final StringEncryptor stringEncryptor;
@Autowired
public CustomEncryptablePropertyResolver(StringEncryptor stringEncryptor) {
this.stringEncryptor = stringEncryptor;
}
@Override
public String resolvePropertyValue(String value) {
if (value != null && value.startsWith("ENC(") && value.endsWith(")")) {
return stringEncryptor.decrypt(value.substring(4, value.length() - 1));
}
return value;
}
}
复制代码
该类负责查抄属性值是否以 "ENC(" 开头并以 ")" 末端,如果是,则对其举行解密。
3. 创建命令行运行器
为了方便地加密密码,我们可以创建一个命令行运行器 EncryptCommandLineRunner:
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class EncryptCommandLineRunner implements CommandLineRunner {
private final StringEncryptor stringEncryptor;
@Autowired
public EncryptCommandLineRunner(StringEncryptor stringEncryptor) {
this.stringEncryptor = stringEncryptor;
}
@Override
public void run(String... args) throws Exception {
String password = "linkdata@2019"; // 替换为你要加密的实际密码
extracted(password);
password = "auditsys_sino@2020_welcome123"; // 替换为你要加密的实际密码
extracted(password);
}
private void extracted(String password) {
String encryptedPassword = stringEncryptor.encrypt(password);
System.out.println("Encrypted Password: ENC(" + encryptedPassword + ")");
}
}
复制代码
这将主动加密指定的密码并在启动时打印出来。
4. 配置 Jasypt
接下来,我们需要配置 Jasypt 以便它知道哪些属性需要被加密/解密。创建配置类 JasyptConfig:
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class JasyptConfig {
private static final Set<String> ENCRYPTED_PROPERTY_KEYS = new HashSet<>(Arrays.asList(
"spring.datasource.druid.master.password",
"spring.redis.password" // 添加其他需要加密处理的属性键
));
@Bean(name = "stringEncryptor")
public PooledPBEStringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword("your-secret-key"); // 使用你自己的密钥
config.setAlgorithm("PBEWithMD5AndDES");
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.ZeroSaltGenerator"); // 禁用盐
config.setStringOutputType("base64");
encryptor.setConfig(config);
return encryptor;
}
@Bean
public EncryptablePropertyResolver encryptablePropertyResolver() {
return new CustomEncryptablePropertyResolver(stringEncryptor());
}
@Bean
public EncryptablePropertyFilter encryptablePropertyFilter() {
return (propertySource, key) -> ENCRYPTED_PROPERTY_KEYS.contains(key);
}
}
复制代码
在这个配置类中,我们界说了一个静态集合 ENCRYPTED_PROPERTY_KEYS 来包含全部需要加密处理的属性键名,并设置了加密算法和其他参数。
5. 运行项目
当你运行项目时,EncryptCommandLineRunner 将会主动实行,并输出加密后的密码。你可以使用这些加密后的值替换配置文件中的明文密码,例如:
spring:
datasource:
druid:
master:
password: ENC(your-encrypted-password)
复制代码
这样,你就乐成地加密了配置文件中的敏感信息,增长了应用步伐的安全性。通过上述步骤,你可以轻松管理和保护应用中的敏感数据。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/)
Powered by Discuz! X3.4