Reputation: 31045
I'm using Spring Boot 1.4.2 and use @ConfigurationProperties
to load my properties to my property bean like this:
@ConfigurationProperties(prefix="my.prop", locations = "classpath:properties/myprop.properties")
@Configuration
public class MyProp {
private String firstName;
private String lastName;
// getters & setters
}
Also I have this property file:
my.prop.firstName=fede
my.prop.lastName=ENC(MzWi5OXKOja3DwA52Elf23xsBPr4FgMi5cEYTPkDets=)
My controller is pretty simple:
@RestController
public class MyController {
@Autowired
private MyProp prop;
@GetMapping("/")
public String get() {
System.out.println(String.format("UserConfig - user: %s, lastName: %s", prop.getFirstName(), prop.getLastName()));
return "something";
}
}
Everything works fine, my properties are loaded and my output is:
2016-11-28 14:36:30,402 INFO 9780 --- [qtp792210014-27] c.c.b.m.c.c.MyController : [OhxxugGR] UserConfig - user: fede, lastName: ENC(MzWi5OXKOja3DwA52Elf23xsBPr4FgMi5cEYTPkDets=)
I confirmed that everything works fine and I wanted to use jasypt to encrypt and use my properties, however I added this dependency to the pom:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>1.9</version>
</dependency>
But jasypt is not decrypting as you can see in the logs. I've read the documentation provided in this jasypt starter but still no luck.
This is my main class:
@SpringBootApplication
@EnableEncryptableProperties
public class ServiceBaseApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceBaseApplication.class, args);
}
}
After testing what stephane-nicoll pointed in his comment, it seems that Jasypt only picks properties located in application.properties
, so how could I use jasypt with properties located outside this file?
Upvotes: 1
Views: 11784
Reputation: 86855
Better directly inject the EncryptablePropertyResolver
and resolve the encrypted key in the setter of the @Value
:
@Autowired
private EncryptablePropertyResolver resolver;
private String yourExternalProperty;
@Value("${your.external.property}")
public void setYourExternalProperty(String value) {
this.yourExternalProperty = resolver.resolvePropertyValue(value);
}
Though I'd be happy if somebody would come up with a more generic solution...
Upvotes: 1
Reputation: 27068
I had the same issue where I was storing my encrypted password in a database and loading that data on startup. But Jasypt cannot decrypt it unless it is in properties file. I had posted the same question to Jasypt guys. Check this link for their reply
https://github.com/ulisesbocchio/jasypt-spring-boot/issues/31#event-752104289
Finally, this is what I ended up doing.
@Configuration
public class JasyptConfig {
@Value("${jasypt.encryptor.password}")
public String encryptDecryptKey; // This is the key I used to encrypt my password
@Bean
public TextEncryptor createTextDecryptor(){
BasicTextEncryptor textDecryptor = new BasicTextEncryptor();
textDecryptor.setPassword(encryptDecryptKey);
return textDecryptor;
}
}
And in my class where I had to decrypt, I did this
@Component
public class PasswordUtil {
private final TextEncryptor textDecryptor;
@Autowired
public PasswordUtil(final TextEncryptor textDecryptor) {
this.textDecryptor = textDecryptor;
}
public String decrypt(String encryptedText){ // This encryptedText is the one like *ENC(....)*
return textDecryptor.decrypt(encryptedText);
}
}
Cheers
Upvotes: 3