加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何解密@ConfigurationProperties bean中使用的属性?

发布时间:2020-12-15 01:27:47 所属栏目:大数据 来源:网络整理
导读:我正在使用Spring Boot 1.2.3,我想了解是否可以在将属性值注入使用@ConfigurationProperties注释的bean之前对其进行解密. 假设我在application.properties文件中有以下内容: appprops.encryptedProperty = ENC(ENCRYPTEDVALUE) 和这样的示例应用程序: pack

我正在使用Spring Boot 1.2.3,我想了解是否可以在将属性值注入使用@ConfigurationProperties注释的bean之前对其进行解密.

假设我在application.properties文件中有以下内容:

appprops.encryptedProperty = ENC(ENCRYPTEDVALUE)

和这样的示例应用程序:

package aaa.bb.ccc.propertyresearch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

import javax.annotation.PostConstruct;

@SpringBootApplication
@EnableConfigurationProperties(PropertyResearchApplication.ApplicationProperties.class)
public class PropertyResearchApplication {

    public static void main(String[] args) {
        SpringApplication.run(PropertyResearchApplication.class,args);
    }

    @ConfigurationProperties("appprops")
    public static class ApplicationProperties {
        private String encryptedProperty;

        @PostConstruct
        public void postConstruct() throws Exception {
            System.out.println("ApplicationProperties --> appprops.encryptedProperty = " + encryptedProperty);
        }

        public String getEncryptedProperty() {
            return encryptedProperty;
        }

        public void setEncryptedProperty(String encryptedProperty) {
            this.encryptedProperty = encryptedProperty;
        }
    }
}

在过去,我使用自定义的PropertySourcesPlaceholderConfigurer来实现此目的,但它需要设置如下结构:

@Component
public class ApplicationProperties {
    @Value("${appprops.enrcyptedProperty}")
    private String encryptedProperty;

    @PostConstruct
    public void postConstruct() throws Exception {
        System.out.println("ApplicationProperties --> appprops.encryptedProperty = " + encryptedProperty);
    }

    public String getEncryptedProperty() {
        return encryptedProperty;
    }
}

虽然这本身并不坏,但我想看看我是否可以利用加密属性的@ConfigurationProperties的细节.

最佳答案
你可以使用org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer
?可以在spring上下文xml文件中添加以下Spring配置.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读