Skip to content

Refer to Spring Cloud Vault in documentation for property encryption #13618

@ESegundoRolon

Description

@ESegundoRolon

Hello,
The following code snippet running on Spring Boot 1.5.12 RELEASE:

public class UnicodePropertiesPropertySourceLoader implements PropertySourceLoader {

    @Override
    public String[] getFileExtensions() {
        return new String[]{"properties"};
    }
    @Override
    public PropertySource<?> load(String name, Resource resource, String profile) throws IOException {
        if (profile == null) {
            Properties properties = new Properties();
            PropertyResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(resource.getInputStream(), "UTF-8"));
            Enumeration<String> keys = bundle.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                properties.setProperty(key, bundle.getString(key));
            }
            if (!properties.isEmpty()) {
                return new PropertiesPropertySource(name, properties);
            }
        }
        return null;
    }
}

Has to be migrated for Spring Boot 2.0.3 RELEASE:

public class UnicodePropertiesPropertySourceLoader implements PropertySourceLoader {

    private static final String PROPERTIES_FILE_EXTENSION = ".properties";

    @Override
    public String[] getFileExtensions() {
        return new String[]{"properties"};
    }

    @Override
    public List<PropertySource<?>> load(String name, Resource resource) throws IOException {
            Properties properties = new Properties();
            PropertyResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(resource.getInputStream(), "UTF-8"));
            Enumeration<String> keys = bundle.getKeys();
            while (keys.hasMoreElements()) {
                String key = keys.nextElement();
                properties.setProperty(key, bundle.getString(key));
            }
            if (!properties.isEmpty()) {
                return Collections
                        .singletonList(new OriginTrackedMapPropertySource(name, properties));
            }

        return null;
    }

After and before the migration, debugging the load method, the properties are loaded just fine for encoded rows, like:

email.hello=!Congrats

But when you load the property using:

@Value("${email.hello}")

The property becomes wrong for the new version of Spring Boot:

Â!Congrats

My spring.factories under META-INF is set to:

# Custom PropertySource Loaders
org.springframework.boot.env.PropertySourceLoader=my.package.UnicodePropertiesPropertySourceLoader

I know that my class UnicodePropertiesPropertySourceLoader is loaded just fine with all the properties encoded to UTF-8, but the problem is when I try to obtain each property with the @Value annotation, this happen for the version of SpringBoot 2.0.3.Release

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions