You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot JDBCTemplate+SQLServerDriver Windows身份认证故障排查

解决方案:Spring Boot 结合 JdbcTemplate 实现 SQL Server Windows NTLM 身份认证

问题根源分析

当前配置存在几个关键错误,导致认证失败:

  1. 依赖坐标错误:spring-jdbc 的 groupId 和 artifactId 写反,可能导致依赖加载异常
  2. 配置类方法拼写错误:setUserName 应为 setUsername,导致用户名未正确传递;Datasource 应为 DataSource(大写D)
  3. URL 参数拼写错误:trustServerCertficate 少写一个字母 i,应为 trustServerCertificate
  4. 用户名格式问题:NTLM 认证需使用 DOMAIN\username 格式,否则会被识别为非信任域用户

步骤1:修正 Maven 依赖配置

确保 spring-jdbc 和 mssql-jdbc 依赖坐标正确,并指定兼容 Java 11 的驱动版本:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <!-- 匹配 Java 11 和 Spring Boot 2.7.x 的稳定版本 -->
    <version>11.2.3.jre11</version>
</dependency>

步骤2:修正 application.yml 配置

修正参数拼写,调整用户名格式:

spring:
  datasource:
    url: jdbc:sqlserver://hostName:portNumber;databaseName=dbName;authenticationScheme=NTLM;integratedSecurity=true;trustServerCertificate=true;
    username: DOMAIN\\username  # 双反斜杠转义,替换为实际域和用户名
    password: yourPassword      # 替换为实际密码

步骤3:修正数据源配置类

推荐使用 Spring Boot 默认的 HikariCP 连接池(性能和兼容性更强),替代简单的 DriverManagerDataSource:

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
        dataSource.setJdbcUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        // 可选:配置连接池参数
        dataSource.setMaximumPoolSize(10);
        dataSource.setConnectionTimeout(30000);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

若坚持使用 DriverManagerDataSource,需修正拼写错误:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class DatabaseConfig {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SqlServerDriver");
        dataSource.setUrl(url);
        dataSource.setUsername(username); // 修正方法名
        dataSource.setPassword(password);
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

额外验证点

  1. 确认 SQL Server 已启用 Windows 身份认证,且目标用户拥有数据库访问权限
  2. 若应用运行在跨域环境,需确保域信任关系正常
  3. Windows 环境下无需额外配置,Linux 环境需补充 Kerberos 配置(当前场景暂不涉及)

内容的提问来源于stack exchange,提问作者lemon chow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 21:57:02