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

Do I need to escape special characters in application.properties?

我在数据源密码字段中有特殊字符,例如 "(双引号)、@、~、!、%、&、}、]。当我 运行 我的 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: "@~!%&=\"\""

像这样向控制台输出值(将代码放入例如 class 注释为 @SpringBootApplication,应用程序启动时将 运行)

@Bean
public CommandLineRunner propertiesTest(@Value("${myapp.entry-with-special-characters}") String myVal) {
    return args -> System.out.printf("Test-output for Whosebug: %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 Whosebug: %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。请记住,密码是机密。

测试时使用的环境:

  • Spring 启动 2.4.1
  • JDK 14.0.2
  • Windows 10 家