SoFunction
Updated on 2025-03-02

Solve the problem of SpringBoot connecting to SqlServer

There is a problem with SpringBoot connecting to SqlServer

The "Encrypt" property is set to "true" and the "trustServerCertificate" property is set to "false"

However, the driver cannot establish a secure connection with SQL Server using Secure Sockets Layer (SSL) encryption:

Error: PKIX path building failed: : unable to find valid certification path to requested target.

The above problem occurred when configuring via yml

spring:
  datasource:
    driver-class-name: 
    username: sa
    password: 123456
    url: jdbc:sqlserver://localhost:1433;databaseName=xxx;

At this time, please note that your SQL statement cannot be written incorrectly, otherwise it will also be reported incorrectly.

After the database name;trustServerCertificate=true and add this to it

Java connection to SqlServer server only supports TLS10

Report an error

Invocation of init method failed; nested exception is : The driver could not establish a secure connection to SQL Server by using Secure Sockets Layer (SSL) encryption. Error: "The server selected protocol version TLS10 is not accepted by client preferences [TLS13, TLS12]

After checking online information, it is said that the reason is that Java believes that TLSv1 is not very safe, so TLSv1 and TLSv1.1 are disabled through configuration, resulting in Java applications using the TLS13 and TLS12 protocols when connecting to the SQLServer client. However, the SQLServer server version may be older or only supported TLS10, resulting in the above error

The reason is that the recent update of the small version of jdk8 has caused the Java security file to change.

How to deal with it

Modify the /usr/java/latest/jre/lib/security/ file content in the basic image and replace it with the previous content

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.