简体   繁体   English

通过 JDBC 从 Linux 机器使用 Windows 身份验证连接到 SQL Server

[英]Connect To SQL Server With Windows Authentication From A Linux Machine Through JDBC

I want to be able to connect to a SQL Server using jdbc and windows authentication.我希望能够使用 jdbc 和 Windows 身份验证连接到 SQL Server。 I saw some answers on the internet saying i should add the following property to the connection string:我在互联网上看到一些答案说我应该将以下属性添加到连接字符串中:

integratedSecurity=true;

And also add并且还添加

sqljdbc_auth.dll

To the java path.到java路径。

But this, as far as i understand applies only when i'm connecting from a Windows machine.但是,据我所知,仅当我从 Windows 机器连接时才适用。 When i try this on a Linux machine i get:当我在 Linux 机器上尝试这个时,我得到:

java.sql.SQLException: This driver is not configured for integrated authentication

My question is how do I do it from a Linux machine.我的问题是如何从 Linux 机器上执行此操作。

Thanks谢谢

Well, eventually I answer my own question: This is not possible to use Windows authentication from a linux machine using the Microsoft JDBC driver.好吧,最终我回答了我自己的问题:使用 Microsoft JDBC 驱动程序的 linux 机器无法使用 Windows 身份验证。 This is possible using the jTDS JDBC driver using the following connection string:这可以通过使用以下连接字符串的 jTDS JDBC 驱动程序实现:

jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;

Thank you all for all the comments谢谢大家的评论

TL;DR TL; 博士

It is not possible to use native Windows Authentication for JDBC connections to MSSQL from a JVM running on Linux.对于从 Linux 上运行的 JVM 到 MSSQL 的 JDBC 连接,无法使用本机 Windows 身份验证。


This MSDN article explains the authentiation methods with JDBC on Linux, potential errors, and available options:这篇 MSDN 文章解释了 Linux 上使用 JDBC 的身份验证方法、潜在错误和可用选项:

https://blogs.msdn.microsoft.com/psssql/2015/01/09/jdbc-this-driver-is-not-configured-for-integrated-authentication/ https://blogs.msdn.microsoft.com/psssql/2015/01/09/jdbc-this-driver-is-not-configured-for-integrated-authentication/

...in the JDBC 4.0 driver, you can use the authenticationScheme connection property to indicate how you want to use Kerberos to connect to SQL. ...在 JDBC 4.0 驱动程序中,您可以使用 authenticationScheme 连接属性来指示您希望如何使用 Kerberos 连接到 SQL。 There are two settings here.这里有两个设置。

  • NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. NativeAuthentication(默认)——它使用 sqljdbc_auth.dll 并且特定于 Windows 平台。 This was the only option prior to the JDBC 4.0 driver.这是 JDBC 4.0 驱动程序之前的唯一选项。

  • JavaKerberos – Makes use of the Java API's to invoke kerberos and does not rely on the Windows Platform. JavaKerberos – 利用 Java API 调用 kerberos,不依赖于 Windows 平台。 This is java specific and not bound to the underlying operating system, so this can be used on both Windows and Linux platforms.这是 Java 特定的,不绑定到底层操作系统,因此它可以在 Windows 和 Linux 平台上使用。

... ...

The following document outlines how to use Kerberos with the JDBC Driver and walks through what is needed to get JavaKerberos working properly.以下文档概述了如何将 Kerberos 与 JDBC 驱动程序一起使用,并介绍了使 JavaKerberos 正常工作所需的条件。

Using Kerberos Integrated Authentication to Connect to SQL Server http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx使用 Kerberos 集成身份验证连接到 SQL Server http://msdn.microsoft.com/en-us/library/gg558122%28v=sql.110%29.aspx

I know this is kind of an older topic but in case Google sends people here:我知道这是一个较旧的话题,但如果谷歌在这里派人:

There are two main JDBC drivers for SQL Server. SQL Server 有两个主要的 JDBC 驱动程序。 One is from Microsoft and the other from jTDS.一个来自 Microsoft,另一个来自 jTDS。 jTDS can, amazingly, connect using Windows auth (NTLM) from other platforms, including Linux, as described here: http://jtds.sourceforge.net/faq.html#windowsAuth .令人惊讶的是,jTDS 可以使用来自其他平台(包括 Linux)的 Windows 身份验证 (NTLM) 进行连接,如下所述: http : //jtds.sourceforge.net/faq.html#windowsAuth It can, of course, also use SQL-authenticated logins.当然,它也可以使用经过 SQL 验证的登录。 SQL-authenticated logins are no harder to use from any OS than any other, so don't forget about those an option.从任何操作系统中使用 SQL 认证登录并不比任何其他操作系统更难使用,所以不要忘记这些选项。

The version provided by Microsoft is the one from which @mjn provided a quote from the documentation. Microsoft 提供的版本是@mjn 从文档中引用的版本。 It is able to connect using Windows authentication by specifying integratedSecurity=true , authenticationScheme=javaKerberos , and authentication=NotSpecified .它可以通过指定integratedSecurity=trueauthenticationScheme=javaKerberosauthentication=NotSpecified使用Windows 身份验证进行连接。

It is tricky to get this working even if you don't go out of your way to find more confusion, so always keep in mind which driver you are using - and tell us in these posts so that you can get more specific help.即使您不去寻找更多的困惑,也很难让它工作,所以请始终记住您使用的是哪个驱动程序 - 并在这些帖子中告诉我们,以便您可以获得更具体的帮助。

For those who are using DBeaver the way to connect to the SQL Server Database is:对于那些使用 DBeaver 的人来说,连接到 SQL Server 数据库的方式是:

In order to connect to the SQL Server from Linux Debian using DBeaver为了使用 DBeaver 从 Linux Debian 连接到 SQL Server

1.- Select SQL Server jTDS driver 1.- 选择 SQL Server jTDS 驱动程序

在此处输入图片说明

2.- Enter the connection information 2.- 输入连接信息

在此处输入图片说明

3.- Go to Driver Properties tab and add the domain, user, password 3.- 转到驱动程序属性选项卡并添加域、用户、密码

在此处输入图片说明

在此处输入图片说明

Just as a note, in some post I found that they needed to change the property USENTLMV2 to TRUE but it worked for me either by putting the USERTLNMV2 in true or false.作为说明,在一些帖子中,我发现他们需要将属性 USENTLMV2 更改为 TRUE,但通过将 USERTLNMV2 设置为 true 或 false 对我有用。

A problem that I found was that when I was trying to connect to the database using my user and password the next error was thrown:我发现的一个问题是,当我尝试使用我的用户名和密码连接到数据库时,抛出了下一个错误:

Login failed.登录失败。 The login is from an untrusted domain and cannot be used with Windows authentication.登录名来自不受信任的域,不能与 Windows 身份验证一起使用。

在此处输入图片说明

This error was thrown because of my user was about to expire.由于我的用户即将到期,因此抛出此错误。 I tried with another AD user and it could connect.我尝试了另一个 AD 用户,它可以连接。

This JDBC URL is validated to work with latest Microsoft SQL Server JDBC driver:此 JDBC URL 经验证可与最新的 Microsoft SQL Server JDBC 驱动程序一起使用:

jdbc:sqlserver://[server]:[port];database=[db\;trustServerCertificate=true;integratedSecurity=true;user=[user without domain];password=[pw];authenticationScheme=NTLM;domain=[domain];authentication=NotSpecified

Example:示例:

jdbc:sqlserver://mysql.myorg.com:1433;database=mydb;trustServerCertificate=true;integratedSecurity=true;user=myuser;password=mypwd;authenticationScheme=NTLM;domain=ad.myorg.com;authentication=NotSpecified

I was able to connect to a SQL Server 2016 Data Mart and JDBC connection Microsoft JDBC Driver using Windows Authentication using the following script on a Ubuntu Linux Docker Image running on Windows 10.我能够使用 Windows 身份验证在 Windows 10 上运行的 Ubuntu Linux Docker 映像上使用以下脚本连接到 SQL Server 2016 数据集市和 JDBC 连接 Microsoft JDBC 驱动程序。

# initializes spark session
from pyspark.sql import SparkSession
spark = SparkSession\
    .builder\
    .master('local[*]')\
    .appName('FDM')\
    .config("spark.driver.extraClassPath","pyspark_jars/*")\
    .config('spark.executor.memory', '4g')\
    .config('spark.driver.memory', '16g')\
    .config('spark.executor.cores', '4')\
    .getOrCreate()

jdbc_url = '''jdbc:sqlserver://SERVER;databaseName=DBNAME;trustServerCertificate=true;integratedSecurity=true;user=USERID;password=PASSWORD;authenticationScheme=NTLM;domain=US;authentication=NotSpecified'''


spark_df = spark.read\
    .format("jdbc")\
    .option("url", jdbc_url)\
    .option("driver","com.microsoft.sqlserver.jdbc.SQLServerDriver")\
    .option("query", 'select top(1000) * from SCHEMA.TABLE')\
    .option("fetchsize", 100000)\
    .load()

spark_df.write.csv('TEST.csv', mode = "overwrite", header=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM