Spring Boot 3中使用Jasypt实现配置文件信息加密
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.jasypt.encryption.StringEncryptor;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public StringEncryptor stringEncryptor() {
// 使用Jasypt默认的Strong加密策略
return Encryptors.strongEncryptor("yourSecretPassword", "mySalt");
}
@Bean
public Resource configFileResource() {
// 指定配置文件路径
return new ClassPathResource("config.encrypted.properties");
}
@Bean
public EncryptedPropertiesFactoryBean encryptedProperties(StringEncryptor stringEncryptor, Resource configFileResource) {
EncryptedPropertiesFactoryBean factoryBean = new EncryptedPropertiesFactoryBean();
factoryBean.setEncryptor(stringEncryptor);
factoryBean.setLocation(configFileResource);
factoryBean.setIgnoreResourceNotFound(true);
return factoryBean;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(Environment environment) {
评论已关闭