英文:
Jasypt not decrypting properties during junit testing, but works fine when spring boot app runs
问题
以下是您要翻译的内容:
我有一个使用Jasypt加密了一些属性的Spring Boot应用程序,并且我通过以下VM选项提供了秘钥:-Djasypt.encryptor.password=secretkey
当我在本地运行应用程序和生产环境中运行时,一切都运行得很完美。问题是,我不知道在进行jUnit测试期间如何解密我在application-test.properties
文件中加密的属性。我甚至将上面相同的VM选项传递给我的jUnit测试。
在jUnit测试期间,我没有收到任何关于为什么无法解密的异常,但我需要解密的属性就是简单地没有被解密。这是否可能,或者是否有某种配置我可以在测试中设置以使用Jasypt解密。
我将包含我下面的代码。我特别运行的测试是针对JavaMailService
方法的。我在application-test.properties
中对密码进行了加密,但测试失败,因为它无法验证用户(因为密码仍然是加密的)。
application-test.properties
:
spring.mail.password=ENC(1NrovUC7+YAzVNhbbpHkRi9TghEPys0aQk1nXonk354=)
主测试类:
@RunWith(SpringRunner.class)
@WebMvcTest(value = { ApiController.class, RestController.class })
@ContextConfiguration(classes = { MyApplication.class, EmailService.class, EmailServiceTestConfig.class })
@ActiveProfiles("test")
class MyApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testSendEmail() throws Exception {
...
}
}
EmailServiceTestConfig
类:
@SpringBootConfiguration
public class EmailServiceTestConfig {
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Bean
public JavaMailSender javaMailService() {
...
}
private Properties getMailProperties() {
...
}
}
我略去了一些不重要的信息,但从EmailServiceTestConfig
类中,我已经检查了从属性文件中检索到的密码,并且它没有被解密。有任何想法,请告诉我!如果我找到解决方案,我也会在这里发布。如果您需要任何其他信息,请告诉我!
英文:
I have a Spring Boot application with a few properties encrypted with Jasypt, and I provide the secret key with the following VM option: -Djasypt.encryptor.password=secretkey
Everything is working perfectly when I run my application locally and in production. The issue is, I'm not sure what I need to do to decrypt the properties I have encrypted in my application-test.properties file I am using during jUnit tests. I'm even passing the same VM option above to my jUnit test.
I'm not receiving any sort of exception during jUnit testing that is telling me why I'm unable to decrypt, but the properties I need decrypted just simply are not being decrypted. Is this even possible or is there some sort of configuration I can setup my tests with to utilize Jasypt decrypting.
I'll include the code I have below. The test I am running in particular is for a JavaMailService method. I have the password encrypted in application-test.properties, and the test fails because it cannot authenticate the user (because password is still encrypted)
application-test.properties:
spring.mail.password=ENC(1NrovUC7+YAzVNhbbpHkRi9TghEPys0aQk1nXonk354=)
Main Test Class:
@RunWith(SpringRunner.class)
@WebMvcTest(value = { ApiController.class, RestController.class })
@ContextConfiguration(classes = { MyApplication.class, EmailService.class, EmailServiceTestConfig.class })
@ActiveProfiles("test")
class MyApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testSendEmail() throws Exception {
...
}
}
EmailServiceTestConfig Class:
@SpringBootConfiguration
public class EmailServiceTestConfig {
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
@Bean
public JavaMailSender javaMailService() {
...
}
private Properties getMailProperties() {
...
}
}
I left out some unimportant information, however from this EmailServiceTestConfig class, I've checked the password retrieved from the properties file, and It has not been decrypted. Any ideas, please let me know! If I find a solution, I'll post here as well. Any additional information you might need, please let me know!
答案1
得分: 2
我遇到了相同的问题,我使用@Import(EnableEncryptablePropertiesConfiguration.class)
来对Test类进行了注解。
是的,使用@SpringBootTest
时一切正常,但加载整个应用程序花费了很多时间。
英文:
I had the same issue, I annotated the Test class with @Import(EnableEncryptablePropertiesConfiguration.class)
.
Yes it was working fine with @SpringBootTest
, but it was taking so much time to load the entire application.
答案2
得分: 0
已解决此问题。我需要使用 @SpringBootTest 注解标注我的应用程序测试类。一旦我这样做了,密码就变成了未加密状态。不确定是如何或为什么,但我相信在运行测试期间,需要将 SpringBootTest 注解与 Spring Boot 环境连接起来。
英文:
Solved this. I needed to annotate my application's test class with @SpringBootTest. Once I did that, the password became decrypted. Not 100% sure how or why, but I believe that the SpringBootTest annotation was needed to connect Jasypt to the Spring Boot environment during the running of the tests.
答案3
得分: 0
我在使用带有 @SpringBootTest 注解的测试时遇到了类似的问题。
当应用程序由测试启动时,它会尝试从属性文件中解密密码。
因为我不想要将密钥放入 application-test.properties 文件中,也不想每次都手动使用密钥运行测试,所以应用程序上下文无法加载,测试失败了。但是我找到了以下的解决方法。
将以下内容放入了我的 application-test.properties 文件中:
jasypt.encryptor.property.prefix=[[[[[
jasypt.encryptor.property.suffix=]]]]]
这样 jasypt 就不会将属性中的密码识别为加密的,因为我覆盖了标准的 ENC( 前缀和 ) 后缀。
现在所有的测试都运行正常了。而且因为我在测试中不需要解密的密码,一切都正常工作。
无论你输入什么作为前缀或后缀,重要的是它不应该是文件中某个属性的开头,这样 jasypt 就不会尝试解密其他内容。
英文:
I had a similar Problem with @SpringBootTest annotated tests.
When the application starts by the test, it tries to decrypt the passwords from the properties-files.
Since I dont want to either put the secret into a application-test.properties file or run the tests everytime with the secret by hand, the application Context couldnt be loaded and the tests failed. But I found the following workaround.
jasypt.encryptor.property.prefix=[[[[[
jasypt.encryptor.property.suffix=]]]]]
I put those into my application-test.properties.
This way jasypt doesn't see my passwords in the properties as encrypted, since i overwrite the standard ENC( prefix and ) suffix.
Now all my tests run. And since I don't need the decrypted passwords in my tests, all works out.
It doesn't matter, what you enter as prefix or suffix, it just mustn't be likely to be the start of a propertie in your files, so jasypt tries to decrypt something else.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论