2015-04-29 13 views
6

Sto usando Spring Boot 1.2.3 e vorrei capire se è possibile decodificare un valore di una proprietà prima dell'iniezione in un bean annotato con @ConfigurationProperties.Come decodificare le proprietà utilizzate nei bean @ConfigurationProperties?

Supponiamo che io ho il seguente in un file application.properties:

appprops.encryptedProperty=ENC(ENCRYPTEDVALUE)

e un'applicazione di esempio in questo modo:

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; 
     } 
    } 
} 

In passato ho usato una consuetudine PropertySourcesPlaceholderConfigurer per raggiungere questo obiettivo ma richiede la creazione di una struttura come la seguente:

@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; 
    } 
} 

Anche se di per sé non è male, mi piacerebbe vedere se posso sfruttare le sottigliezze di @ConfigurationProperties con proprietà crittografate.

+2

Hai visto questo: http://stackoverflow.com/questions/24451110/creating-a-custom-jasypt-propertysource-in-springboot – koe

+0

OP trovato la risposta su http://stackoverflow.com/a/24486190/4094797. Si prega di controllare il link per trovare la soluzione. – user2339071

risposta

0

è possibile utilizzare org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer può essere aggiunto seguente configurazione Spring nel file xml di contesto di primavera.

<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> 
0

Basta rilasciare il seguente file nel progetto Spring e implementare il metodo di decrittografia personalizzato.

@Component 
public class CmtEncryptedPropertyConfigurer extends PropertySourcesPlaceholderConfigurer { 

private ConfigurableEnvironment environment; 

@Override 
public void setEnvironment(Environment environment) { 
    super.setEnvironment(environment); 
    this.environment = (ConfigurableEnvironment) environment; 
} 
@Override 
protected void loadProperties(Properties props) throws IOException { 
    this.localOverride = true; 
    for (PropertySource<?> propertySource : environment.getPropertySources()) { 
     if (propertySource instanceof EnumerablePropertySource) { 
      String[] propertyNames = ((EnumerablePropertySource) propertySource).getPropertyNames(); 
      for (String propertyName : propertyNames) { 
       String propertyValue = propertySource.getProperty(propertyName).toString(); 
       // put logic to see if decryption required for thsi name/value 
       // decrypt here 
       String decryptedValue = decrypt(propertyValue); 
       // set value here 
       props.setProperty(propertyName, decryptedValue); 
      } 
     } 
    } 
}} 
0

PropertyResourceConfigurer ha un metodo convertPropertyValue che è possibile sovrascrivere per questo scopo.

https://docs.spring.io/autorepo/docs/spring/4.1.6.RELEASE/javadoc-api/org/springframework/beans/factory/config/PropertyResourceConfigurer.html#convertPropertyValue-java.lang.String-

public class EncryptedPropertySourcedPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer { 

    @Override 
    protected String convertPropertyValue(String originalValue) { 
    if(originalValue.startswith("ENC") { 
     return decrypt(originalValue.subString(4, originalValue.length() - 1); 
    } 
    return originalValue; 
    } 
} 
Problemi correlati