【问题标题】:Unable to connect to Azure Synapse DB via JDBC using authentication=ActiveDirectoryPassword无法使用 authentication=ActiveDirectoryPassword 通过 JDBC 连接到 Azure Synapse DB
【发布时间】:2021-11-30 17:49:30
【问题描述】:

我在尝试使用 JDBC 和 authentication=ActiveDirectoryPassword 连接到 Azure Synapse DB 时遇到问题。

我正在使用 Java 8 进行开发。以下是我正在使用的 pom 依赖项:

<dependencies>
   <dependency>
      <groupId>com.microsoft.sqlserver</groupId>
      <artifactId>mssql-jdbc</artifactId>
      <version>9.4.0.jre8</version>
  </dependency>

  <dependency>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>msal4j</artifactId>
     <version>1.11.0</version>
  </dependency>
</dependencies>

这是我用来连接 Synapse DB 的代码:

SQLServerDataSource sqlServerDataSource = new SQLServerDataSource();

sqlServerDataSource.setServerName("someserver.database.windows.net");

sqlServerDataSource.setDatabaseName("somedbname");

sqlServerDataSource.setUser("someuser@some.com");

sqlServerDataSource.setPassword("somepwd");

sqlServerDataSource.setAuthentication("ActiveDirectoryPassword");

sqlServerDataSource.setEncrypt(true);

sqlServerDataSource.setTrustServerCertificate(true);

sqlServerDataSource.setUseBulkCopyForBatchInsert(true);

Connection connection = sqlServerDataSource.getConnection();

这是我为上述代码引用的 MSFT 链接:

https://docs.microsoft.com/en-us/sql/connect/jdbc/connecting-using-azure-active-directory-authentication?view=sql-server-ver15#connecting-using-activedirectorypassword-authentication-mode

问题是,对sqlServerDataSource.getConnection() 的调用失败并出现以下错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Failed to authenticate the user someuser@some.com in Active Directory (Authentication=ActiveDirectoryPassword). javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.microsoft.sqlserver.jdbc.SQLServerMSAL4JUtils.getCorrectedException(SQLServerMSAL4JUtils.java:228)
    at com.microsoft.sqlserver.jdbc.SQLServerMSAL4JUtils.getSqlFedAuthToken(SQLServerMSAL4JUtils.java:65)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getFedAuthToken(SQLServerConnection.java:4751)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.onFedAuthInfo(SQLServerConnection.java:4724)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.processFedAuthInfo(SQLServerConnection.java:4680)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onFedAuthInfo(tdsparser.java:289)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:125)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:37)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:5560)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:4289)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:88)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:4227)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7417)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:3488)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2978)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2628)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2471)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1470)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnectionInternal(SQLServerDataSource.java:1317)
    at com.microsoft.sqlserver.jdbc.SQLServerDataSource.getConnection(SQLServerDataSource.java:100)

我不确定除了上面提到的 2 之外我是否需要任何其他依赖项。

如果有人遇到过类似的问题并愿意分享他们的解决方案,那真的很有帮助。

提前致谢。

【问题讨论】:

    标签: maven jdbc java-8 azure-active-directory azure-synapse


    【解决方案1】:

    根据错误,问题似乎与用户帐户或证书有关,所以请尝试使用,例如 sqlServerDataSource.setTrustServerCertificate(false);在您的代码中并尝试连接

    并确保为您的服务器设置 azure ADadmin 在 azure 门户中>>搜索 sql 服务器>>并单击您的服务器并转到设置下的活动目录管理>>选择设置管理员。>>在添加管理页面中,搜索用户,选择用户或组成为管理员,然后选择>>在 Active Directory 管理页面的顶部,选择保存。(更改将需要几分钟)

    其他选项就像您可以使用包含用户一样,为此包含的用户数据库必须存在,并且代表指定 Azure AD 用户或指定 Azure AD 用户所属的组之一的包含数据库用户必须存在于数据库,并且必须具有 CONNECT 权限。为此,您需要创建一个包含的用户,并且需要将该用户映射到 azure AD。

    参考Create contained users mapped to Azure AD identities

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 1970-01-01
      • 2022-10-19
      • 2020-12-01
      • 2021-12-22
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多