0

I have been working on a scenario wherein I want to decrypt an entry in the application.yml .The value I want to encrypt, keep in yml file and then decrypt while the module comes up

@Value("${app.datasource.password}")
private String password; 

I was trying to figure out how @Value works internally so that I could modify it to include this feature. If there is other way possible say by introducing custom annotation and AnnotationProcessor, it would also help.

1 Answer 1

3

@Value Annotation just takes a Spring SPEL expression and evaluates it to set the result value for the annotated field.

For your use case you can write a Decrypter object and write a method to decrypt your data and use it as below.

@Value("#{passwordDecrypter.decrypt()}")
private String password; 

Write a Bean for doing decryption

@Component
public class PasswordDecrypter{

    @Value("${app.datasource.password}")
    private String password; 

    public String decrypt(){
        // Return decrypted value
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

This helps. However, is there a simple annotation based approach which doesn't necessarily use @Value or other Spring annotations so that I can extend this solution to simple java programs
You can try this. github.com/ulisesbocchio/jasypt-spring-boot But remember that this is not provided from Spring.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.