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

标签 spring spring-boot 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: "@~!%&=\"\""

像这样将值输出到控制台(将代码放入例如用 @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

enter image description here

回答问题

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/

相关文章:

java - 如何在没有任何请求、 session 等的情况下获取上下文?

java - 限制 spring-data-rest 返回的文档

java - Spring MVC 域对象处理最佳实践

spring-boot - 使用 OAuth2 身份验证启用 Spring Boot Health Actuator

java - Spring在 Multi-Tenancy 环境中为占位符配置application.properties

java - 如何在没有方法参数的方法上使用 @cacheable 注释的键

java - NoClassDefFoundError : NestedIOException when running tests with Spring Boot 3. 0.0-M4/Spring 6.0.0-M5

java - 使用 openapi 3.0、aws-cognito oauth2 配置 Springboot 时遇到问题

java - 在运行时使用 intellij 在 Spring boot application.yml 文件中注入(inject)占位符值