我需要转义application.properties中的特殊字符吗?

tquggr8v  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(1551)

我有一些特殊的字符,比如“(双引号),@,~,!,%,&,},]在数据源密码字段中。当我运行我的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、jdk1.8和mssqlserver。
谢谢。

6uxekuva

6uxekuva1#

application.properties:指定的字符不需要转义。
application.yml:用 " 和逃生装置 " 这样地 \" 注意:application.yml不是op的问题。请使用application.propertiesorapplication.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 开机:休眠和飞道开机顺序

问题的答案
有没有办法查看spring尝试连接时使用的密码?
可以更换钥匙 myapp.entry-with-special-characters 在上面的bean中使用application.properties中的相关键。请记住,密码是秘密。
测试期间使用的环境:
Spring Boot2.4.1
jdk 14.0.2版
windows 10主页

相关问题