Spring读取properties文件加密解密

文章概要:数据源配置文件database.properties里面填写加密后的密文,程序读取配置文件的时候进行相应的解密操作后再注入使用。此功能主要用于生产环境。
使用的spring版本:4.2.5,配置采用一贯的作风javaConfig
首先在springmvc配置文件当中声明一个PropertyPlaceholderConfigurer类型的bean:
    @Bean
    public PropertyPlaceholderConfigurer datasourceProperties() {
        PropertyEncrypt propertyEncrypt = new PropertyEncrypt();
        propertyEncrypt.setLocations(new ClassPathResource("database.properties"));
        return propertyEncrypt;
    }
然后定义用于读取配置文件时解密的类继承于PropertyPlaceholderConfigurer:

import com.core.shared.StringUtils;
import com.core.shared.encrypt.PEncryptUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.ArrayList;
import java.util.List;

public class PropertyEncrypt extends PropertyPlaceholderConfigurer {

    private static final List<String> encryptKey = new ArrayList<>();

    static {
        encryptKey.add("master.jdbc.url");
        encryptKey.add("master.jdbc.username");
        encryptKey.add("master.jdbc.password");
        encryptKey.add("slave.jdbc.url");
        encryptKey.add("slave.jdbc.username");
        encryptKey.add("slave.jdbc.password");
    }

    public void addEncryptKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            encryptKey.add(key);
        }
    }

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (encryptKey.contains(propertyName)) {
            propertyValue = PEncryptUtil.decrypt(propertyValue);
        }
        return propertyValue;
    }

}
PEncryptUtil是自己定义的加密解密类,最后在使用的时候使用@Value注入:
    @Value("${master.jdbc.driverClassName}")
    private String masterDriverClassName;
    @Value("${master.jdbc.url}")
    private String masterUrl;
    @Value("${master.jdbc.username}")
    private String masterUsername;
    @Value("${master.jdbc.password}")
    private String masterPassword;
配置文件里直接用密文就好了:
master.jdbc.url=abc
master.jdbc.username=abc
master.jdbc.password=abc
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring可以通过`PropertyPlaceholderConfigurer`来读取properties文件。 1. 在Spring配置文件中添加`PropertyPlaceholderConfigurer`的bean: ```xml <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> ``` 2. 在properties文件中定义属性: ```properties jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=123456 ``` 3. 在Spring配置文件中使用属性: ```xml <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> ``` 这样就可以通过Spring读取properties文件中的属性了。 ### 回答2: Spring 是一个广泛使用的框架,受欢迎的一个原因是其简化了很多任务。其中之一就是读取 properties 文件。在 Spring 中,我们不必自己编写代码从文件读取属性。我们可以利用 Spring 的功能来自动读取属性,使得我们可以更加专注于应用程序的业务逻辑,并且遵循 Spring 管理原则。 Spring 提供了多种读取 properties 文件的方式。下面是其中的几种常见方式: 1. 使用 @Value 注解 @Value 注解本质上将属性文件的值注入到我们的 Java 类中。例如,我们可以在 Spring 的配置文件中定义以下属性值: application.properties ```properties app.name=My Application app.version=1.0 ``` 然后在我们的 Java 类中,在需要的属性前面加上 @Value 注解即可。 ```java @Component public class MyComponent { @Value("${app.name}") private String appName; @Value("${app.version}") private String appVersion; // ... } ``` 2. 使用 PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer 是一个特殊的 bean,它读取属性文件并将属性值注入到其他 bean 中。配置方式如下: ```xml <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/application.properties</value> </list> </property> </bean> ``` 然后,在 Java 类中可以使用 @Value 注解获取属性值。 3. 配置文件的统一管理 Spring 还提供了一个功能来统一管理配置文件。每个应用程序可能需要多个配置文件,所以 Spring 提供了一种方法来将这些配置文件合并为一个层次结构。这种方式称为 “Environment Abstraction”,它允许应用程序使用一组由多个配置文件组成的 “属性源”。 ```java @Configuration @PropertySource("classpath:/application.properties") public class AppConfig { @Autowired Environment env; @Bean public MyComponent myComponent() { MyComponent component = new MyComponent(); component.setAppName(env.getProperty("app.name")); component.setAppVersion(env.getProperty("app.version")); return component; } } ``` 以上是 Spring 读取 properties 文件的常见方式。开发者可以根据自己的需求选择不同的方式。无论选择哪种方式,Spring 已经为我们创建了属性文件读取的底层机制和简化方式,使得我们更加便利地开发。 ### 回答3: Spring读取properties文件是很常见的操作,通常我们会在配置文件中声明需要读取文件路径,并通过注入的方式将文件中的内容读取Spring的环境中。以下是更详细的步骤: 1. 确定properties文件的存储位置和名称。可以将其放置在classpath下或者指定文件路径。这里假设放在classpath下,文件名为config.properties。 2. 在Spring的配置文件中使用以下语句引用这个properties文件: ``` <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean> ``` 这里使用了Spring提供的“PropertyPlaceholderConfigurer”类。这个类允许我们将properties文件的内容注入到Spring的环境中。 3. 在需要读取properties文件的类中注入这个“PropertyPlaceholderConfigurer”对象,并使用以下语句读取其中的值: ``` @Value("${key}") private String value; ``` 其中,key是properties文件中的键名,value则为其对应的值。 另外,还可以通过“ResourceBundleMessageSource”类直接读取properties文件中的文本资源,例如: ``` ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("config"); String message = messageSource.getMessage("key", null, Locale.getDefault()); ``` 这里使用了Spring提供的“ResourceBundleMessageSource”类,将properties文件中的文本资源转换为字符串类型的message。 总之,读取properties文件Spring开发中一个比较基础的操作,熟练掌握此技能可以提高代码开发效率,并且为系统开发提供清晰的配置方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值