Java 通过 JDBC 从 Linux 机器使用 Windows 身份验证连接到 SQL Server
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37835929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connect To SQL Server With Windows Authentication From A Linux Machine Through JDBC
提问by zuckermanori
I want to be able to connect to a SQL Server using jdbc and windows authentication. I saw some answers on the internet saying i should add the following property to the connection string:
我希望能够使用 jdbc 和 Windows 身份验证连接到 SQL Server。我在互联网上看到一些答案说我应该将以下属性添加到连接字符串中:
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. When i try this on a Linux machine i get:
但是,据我所知,仅当我从 Windows 机器连接时才适用。当我在 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
谢谢
回答by mjn
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 的身份验证方法、潜在错误和可用选项:
...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. There are two settings here.
NativeAuthentication (default) – This uses the sqljdbc_auth.dll and is specific to the Windows platform. This was the only option prior to the JDBC 4.0 driver.
JavaKerberos – Makes use of the Java API's to invoke kerberos and does not rely on the Windows Platform. This is java specific and not bound to the underlying operating system, so this can be used on both Windows and Linux platforms.
...在 JDBC 4.0 驱动程序中,您可以使用 authenticationScheme 连接属性来指示您希望如何使用 Kerberos 连接到 SQL。这里有两个设置。
NativeAuthentication(默认)——它使用 sqljdbc_auth.dll 并且特定于 Windows 平台。这是 JDBC 4.0 驱动程序之前的唯一选项。
JavaKerberos – 利用 Java API 调用 kerberos,不依赖于 Windows 平台。这是 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
回答by zuckermanori
Well, eventually I answer my own question: This is not possible to use Windows authentication from a linux machine using the Microsoft JDBC driver. This is possible using the jTDS JDBC driver using the following connection string:
好吧,最终我回答了我自己的问题:使用 Microsoft JDBC 驱动程序的 linux 机器无法使用 Windows 身份验证。这可以通过使用以下连接字符串的 jTDS JDBC 驱动程序实现:
jdbc:jtds:sqlserver://host:port;databaseName=dbname;domain=domainName;useNTLMv2=true;
Thank you all for all the comments
谢谢大家的评论
回答by s-k-y-e---c-a-p-t-a-i-n
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. One is from Microsoft and the other from jTDS. jTDS can, amazingly, connect using Windows auth (NTLM) from other platforms, including Linux, as described here: http://jtds.sourceforge.net/faq.html#windowsAuth. It can, of course, also use SQL-authenticated logins. SQL-authenticated logins are no harder to use from any OS than any other, so don't forget about those an option.
SQL Server 有两个主要的 JDBC 驱动程序。一个来自 Microsoft,另一个来自 jTDS。令人惊讶的是,jTDS 可以使用来自其他平台(包括 Linux)的 Windows 身份验证 (NTLM) 进行连接,如下所述:http: //jtds.sourceforge.net/faq.html#windowsAuth。当然,它也可以使用经过 SQL 验证的登录。从任何操作系统中使用 SQL 认证登录并不比任何其他操作系统更难使用,所以不要忘记这些选项。
The version provided by Microsoft is the one from which @mjn provided a quote from the documentation. It is able to connect using Windows authentication by specifying integratedSecurity=true
, authenticationScheme=javaKerberos
, and authentication=NotSpecified
.
Microsoft 提供的版本是@mjn 从文档中引用的版本。它能够使用Windows身份验证通过指定连接integratedSecurity=true
,authenticationScheme=javaKerberos
和authentication=NotSpecified
。
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.
即使您不去寻找更多的困惑,也很难让它工作,所以请始终记住您使用的是哪个驱动程序 - 并在这些帖子中告诉我们,以便您可以获得更具体的帮助。
回答by mavi
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 用户,它可以连接。