Cen*_*nul 6 java encryption spring spring-boot
我正在使用Spring Boot 1.2.3,我想了解是否可以在将属性值注入带注释的bean之前对其进行解密@ConfigurationProperties.
假设我在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在过去,我使用自定义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;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这本身并不坏,但我想看看我是否可以利用@ConfigurationProperties加密属性的细节.
您可以org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer
在 spring context xml 文件中添加以下 Spring 配置。
<context:property-placeholder location="classpath:application.properties"/>
<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer">
<constructor-arg ref="configurationEncryptor" />
<property name="location" value="classpath:application.properties" />
</bean>
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
<property name="algorithm" value="PBEWithMD5AndDES" />
<property name="password" value="password" />
</bean>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2187 次 |
| 最近记录: |