0

com.microsoft.sqlserver.jdbc.SQLServerException:无法在 Active Directory 中验证用户 fakeaccount@gmail.com (Authentication=ActiveDirectoryPassword)。在 com.microsoft.sqlserver.jdbc.SQLServerADAL4JUtils.getSqlFedAuthToken(SQLServerADAL4JUtils.java:62) ~[mssql-jdbc-8.4.1.jre8.jar:na] 在 com.microsoft.sqlserver.jdbc.SQLServerConnection.getFedAuthToken(SQLServerConnection. java:4442) ~[mssql-jdbc-8.4.1.jre8.jar:na] at com.microsoft.sqlserver.jdbc.SQLServerConnection.onFedAuthInfo(SQLServerConnection.java:4415) ~[mssql-jdbc-8.4.1.jre8 .jar:na]

4

1 回答 1

0

正如Jatin所建议的那样。您可以参考 Azure Active Directory 身份验证文档,您可以在其中找到不同的身份验证方法,下面是连接 Azure Active Directory 和 MSI 身份验证方法的示例。

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.microsoft.sqlserver.jdbc.SQLServerDataSource;

public class AAD_MSI {
    public static void main(String[] args) throws Exception {

        SQLServerDataSource ds = new SQLServerDataSource();
        ds.setServerName("aad-managed-demo.database.windows.net"); // Replace with your server name
        ds.setDatabaseName("demo"); // Replace with your database name
        ds.setAuthentication("ActiveDirectoryMSI");
        // Optional
        ds.setMSIClientId("94de34e9-8e8c-470a-96df-08110924b814"); // Replace with Client ID of User-Assigned Managed Identity to be used

        try (Connection connection = ds.getConnection();
                Statement stmt = connection.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT SUSER_SNAME()")) {
            if (rs.next()) {
                System.out.println("You have successfully logged on as: " + rs.getString(1));
            }
        }
    }
}
于 2022-02-02T10:53:45.203 回答