【问题标题】:Microsoft JDBC driver having strange behaviour with java codeMicrosoft JDBC 驱动程序对 java 代码有奇怪的行为
【发布时间】:2020-04-23 14:45:54
【问题描述】:

我尝试使用 java 代码使用 Microsoft JDBC 驱动程序在启用了 SSL 协议的 SQL 实例和使用 NTLMV2 协议的 SQL Server 上获得连接。

我正在使用 Windows 身份验证来获取连接,这将同时生效 SSL 和 NTLMV2 协议。

但奇怪的是我们能够建立连接 java JDBC 客户端,而无需为 NTLMV2 和 SSL 设置 java 属性。

谁能帮我弄清楚为什么使用 Microsoft JDBC 驱动程序会发生这种奇怪的事情。

请找到我的 java 代码 sn-p,它使用连接 url 与启用了 SSL 和 NTLMV2 协议的 SQL 实例建立连接。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;


public class SqlJdbcConnection 
{
    String dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String connectionUrl = "";

    private void connect()
    {
        Connection conn=null;
        Statement stmt=null;
        ResultSet rset=null;
        try 
        {
            Class.forName(dbDriver);
        } 
        catch (ClassNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return;
        }  
        try
        {           
            connectionUrl = "jdbc:sqlserver://192.168.11.215:1433;databaseName=master;integratedSecurity=true";         
            Properties infoProperties=new Properties();
            infoProperties.put("UserName","administrator");
            infoProperties.put("Password","abc098ABC");
            infoProperties.put("domain","mas");
            infoProperties.put("authenticationScheme","NTLM");
            conn = DriverManager.getConnection(connectionUrl, infoProperties);
            if(conn==null)
            {
                System.out.println("Connection is null");
                return;
            }
            stmt=conn.createStatement();
            rset = stmt.executeQuery("select @@version");
            while(rset.next())
            {
                System.out.println(rset.getString(1));
            }

        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        finally
        {
            if(rset!=null)
            {
                try 
                {
                    rset.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
            if(stmt!=null)
            {
                try 
                {
                    stmt.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
            if(conn!=null)
            {
                try 
                {
                    conn.close();
                } catch (Exception e2) {
                    // TODO: handle exception
                }
            }
        }

    }
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        SqlJdbcConnection sqljdbc=new SqlJdbcConnection();
        sqljdbc.connect();      }
}

【问题讨论】:

  • 到底是什么问题?您指定的属性将其配置为使用 NTLMv2,而 NTLMv2 与 SSL 无关。

标签: java sql-server jdbc driver


【解决方案1】:

JDBC 规范不强制要求支持 SSL/TLS。所以你不能指望每个驱动程序都有它。

以下属性也用于 NTLM V2 身份验证:

domain = domainName (optional)
user = userName
password = password
integratedSecurity = true

您无意中已经在使用 NTLM v2 身份验证

【讨论】:

    【解决方案2】:

    您需要通过以下方式将 sqljdbc_auth.dll 添加到运行时 java -Djava.library.path={path to sqljdbc_auth.dll} 使用这个,你不需要提供任何用户名,因为它是你运行Java VM的用户。

    您可以在 Microsoft JDBC 驱动程序包中找到此 DLL。

    如果您的用户是 SQL-Server 用户,那么您不需要 IntegratedSecurity。

    【讨论】:

      猜你喜欢
      • 2013-08-12
      • 2016-07-24
      • 2019-10-02
      • 2017-01-09
      • 2011-12-12
      • 2011-07-10
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      相关资源
      最近更新 更多