微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在使用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配置.

property-placeholder location="classpath:application.properties"/>


figurer">
        figurationEncryptor" />
        sspath:application.properties" />
    figurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐