1、报错信息

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is com.microsoft.sqlserver.jdbc.SQLServerException: 驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。错误:“sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”。 ClientConnectionId:fc5064da-6aa8-45a7-8666-69c2b0ac6b90] with root cause
 

1.1、问题:

使用navicat连接sqlserver正常连接,但使用idea连接时报连接协议(SSL/TLS)对不上,也就是证书验证失败。

这时需要在数据源配置时候添加跳过证书验证

1.2、解决:

在yml配置数据源的地方添加  ;encrypt=true;trustServerCertificate=true;

例如:url: jdbc:sqlserver://数据库ip地址:1433;DatabaseName=库名;encrypt=true;trustServerCertificate=true;

注:文章尾部有详细数据源配置。

2、springboot项目中使用sql-server数据库需要做的工作:

2.1、maven依赖注入:

<dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
</dependency>

2.2、application.yml文件数据源配置:

spring:
        datasource:
                driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
                url: jdbc:sqlserver://数据库ip地址:1433;DatabaseName=库名;encrypt=true;trustServerCertificate=true;
                username: root
                password: root
                maxActive: 20
                initialSize: 1
                maxWait: 60000
                minIdle: 1
                timeBetweenEvictionRunsMillis: 60000
                minEvictableIdleTimeMillis: 300000
                validationQuery: select 1
                testWhileIdle: true
                testOnBorrow: true
                testOnReturn: true
                poolPreparedStatements: true
                maxOpenPreparedStatements: 20

2.3、工具版本:

idea2023、maven3.9、sql-server2022、SQL Server Management Studio(sql-server可视化工具)、jdk17

更多推荐