1

JavaMailSender is being auto configured in my Spring Boot application. How can I use annotations to allow encrypted values in my properties file for the properties "spring.mail.username"and "spring.mail.password" using the Jasypt library? please help.

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SimpleMailController {

@Autowired
private JavaMailSender sender;
Abdullah
  • 139
  • 3
  • 17

1 Answers1

1

I was able to crack it.

  1. Add the annotation @EnableEncryptableProperties to my Application class.
  2. Add jasypt spring boot starter dependency in gradle script -

compile('com.github.ulisesbocchio:jasypt-spring-boot-starter:2.0.0')

All the properties used in my application now support encrypted values by default.

Abdullah
  • 139
  • 3
  • 17
  • Your answer seems to be missing something. Don't you also need to specify the property file with "@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties")})"? Even when I do this, JavaMailSender doesn't get the password. Did you also set a system environment variable for jasypt.encryptor.password? Thanks! – Mike Mar 24 '19 at 18:55
  • I did not need to use the annotation @EncryptablePropertySources in my application. The property jasypt.encryptor.password should ideally be passed as a runtime variable. For testing purpose, add it in the same properties file you want to encrypt. – Abdullah Mar 26 '19 at 05:37
  • I would like to add one more observation. The above solution did not work when jasypt version was 3.0.3. When I downgraded to 2.0.0 the solution worked. Refer to comments – A-K Jun 05 '20 at 09:16