我在数据源密码字段中有特殊字符,例如“(双引号)、@、~、!、%、&、}、]。当我运行我的 springboot 应用程序并尝试连接到数据库时,我遇到-
com.microsoft.sqlserver.jdbc.SQLServerException: The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "problem accessing trust store"
Caused by: java.security.KeyStoreException: problem accessing trust store
Caused by: java.io.IOException: Keystore was tampered with, or password was incorrect
Caused by: java.security.UnrecoverableKeyException: Password verification failed
我的问题是:
(1) 有没有办法查看Spring用来尝试连接的密码是什么?
(2) 我需要对application.properties
中密码中的任何特殊字符进行转义吗?
我能够使用独立的 java 程序连接到数据库,但我不得不转义密码中的双引号。我在后端使用 Springboot-2.0.4、Flyway-5.2.4、jdk 1.8 和 MS-SQL 服务器。
谢谢。
最佳答案
application.properties:您指定的字符不需要转义。
如果您使用例如一个反斜杠,您需要用另一个反斜杠对其进行转义,如下所示:C:\\Development\\mydir
application.yml:用 "
包围值并转义嵌入的 "
像这样 \"
注意:application.yml 不是 OP 的问题。使用 application.properties 或 application.yml,不要同时使用。
在 application.properties
中使用此条目进行了测试和验证myapp.entry-with-special-characters=@~!%&=""
并在 application.yml
中使用此条目再次测试myapp:
entry-with-special-characters: "@~!%&=\"\""
像这样将值输出到控制台(将代码放入例如用 @SpringBootApplication
注释的类,将在应用程序启动时运行)
@Bean
public CommandLineRunner propertiesTest(@Value("${myapp.entry-with-special-characters}") String myVal) {
return args -> System.out.printf("Test-output for StackOverflow: %s\n", myVal);
}
使用 Flyway,试试这个
/**
* Override default flyway initializer to do nothing
*/
@Bean
FlywayMigrationInitializer flywayInitializer(Flyway flyway, @Value("${myapp.entry-with-special-characters}") String myVal) {
System.out.printf("Test-output for StackOverflow: %s\n", myVal);
return new FlywayMigrationInitializer(flyway, (f) ->{} );
}
Spring Boot: Hibernate and Flyway boot order
回答问题
Is there a way to view what password is Spring using to attempt the connection?
可以用 application.properties 中的相关键替换上述 bean 中的键 myapp.entry-with-special-characters
。请记住,密码是 secret 。
测试时使用的环境:
- Spring Boot 2.4.1
- JDK 14.0.2
- Windows 10 家庭版
关于spring - 我需要转义 application.properties 中的特殊字符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65797582/