Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package eu.hinsch.spring.propertiesdecrypter;
 
 import  org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
 
 import java.util.List;
import static java.util.stream.Collectors.toList;

Created by lh on 02/04/15.
public class DecryptingPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer {
    static final String PASSWORD_PROPERTY = "propertyDecryption.password";
    public static final String DEFAULT_ALGORITHM = "PBEWithMD5AndDES";
    public static final String PREFIX_KEY = "propertyDecryption.prefix";
    private String prefix;
    @Override
    public void setEnvironment(final Environment environment) {
        initializeEncrypter(environment);
         = environment.getProperty("{encrypted}");
        final ConfigurableEnvironment configurableEnvironment = (ConfigurableEnvironmentenvironment;
        final MutablePropertySources propertySources = configurableEnvironment.getPropertySources();
        List<StringencryptedKeys = getKeysOfEncryptedPropertyValues(environmentpropertySources);
        addDecryptedValues(environmentpropertySourcesencryptedKeys);
        super.setEnvironment(environment);
    }
    private void initializeEncrypter(Environment environment) {
        .setAlgorithm(environment.getProperty("propertyDecryption.algorithm"));
    }
    private List<StringgetKeysOfEncryptedPropertyValues(Environment environmentMutablePropertySources propertySources) {
        Stream<PropertySource<?>> stream = getPropertySourceStream(propertySources);
        return stream.filter(source -> source instanceof MapPropertySource)
                .map(source -> (MapPropertySourcesource)
                .map(PropertySource::getSource)
                .flatMap(map -> map.entrySet().stream())
                .map(Map.Entry::getKey)
                .filter(key -> !.equals(key))
                .filter(key -> isEncrypted(environment.getProperty(key)))
                .collect(toList());
    }
    private Stream<PropertySource<?>> getPropertySourceStream(final MutablePropertySources propertySources) {
        final Iterator<PropertySource<?>> iterator = propertySources.iterator();
        Iterable<PropertySource<?>> iterable = () -> iterator;
        return StreamSupport.stream(iterable.spliterator(), false);
    }
    private void addDecryptedValues(Environment environmentMutablePropertySources propertySourcesList<StringencryptedKeys) {
        // TODO collect to properties?
        final Properties decryptedValues = new Properties();
        encryptedKeys.stream().forEach(
                key -> {
                    String value = environment.getProperty(key);
                    decryptedValues.put(keydecryptPropertyValue(value));
                }
        );
        propertySources.addFirst(new PropertiesPropertySource("decryptedValues"decryptedValues));
    }
    private String decryptPropertyValue(String encryptedPropertyValue) {
        String cypher = getCypher(encryptedPropertyValue);
        return .decrypt(cypher);
    }
    private boolean isEncrypted(Object propertyValue) {
        return propertyValue != null && propertyValue instanceof String && ((String)propertyValue).startsWith();
    }
    private String getCypher(String encryptedPropertyValue) {
        return encryptedPropertyValue.substring(.length());
    }
New to GrepCode? Check out our FAQ X