Azure 连接无法连接证书错误

Azure Connection Can Not Connect Certificate Error

我很难尝试针对 Azure 的特定租户 ID 进行身份验证。我使用的代码如下:

public abstract class Azure
{
    private final static String GRAPH = "https://graph.windows.net/";
    private Logger objLogger;
    private String strAccessToken;
    private String strTenantID;
    private String strLogin;
    private String strAuthorize;
    private String strGraph;
    private String strApplicationID;
    private String strUsername;
    private String strPassword;
    public String getAccessToken() throws InvalidKeyException, MalformedURLException, ServiceUnavailableException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InterruptedException, ExecutionException
    {
        if (this.strAccessToken == null)
        {
            this.setAccessToken();
        }
        return this.strAccessToken;
    }
    private void setAccessToken() throws MalformedURLException, InterruptedException, ExecutionException, ServiceUnavailableException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
    {
        AuthenticationContext objContext;
        AuthenticationResult objToken;
        ExecutorService objService;
        Future<AuthenticationResult> objFuture;
        objService = null;
        objToken = null;
        try
        {
            objService = Executors.newFixedThreadPool(1);
            objContext = new AuthenticationContext(this.getAuthorize(), false, objService);
            objFuture = objContext.acquireToken(GRAPH, this.getApplicationID(), this.getUsername(), this.getPassword(), null);
            objToken = objFuture.get();
            this.getLogger().info("Connection to Azure ".concat(this.getClass().getSimpleName().toLowerCase()).concat(" successfully stablished"));
        }
        finally
        {
            objService.shutdown();
        }
        if (objToken == null)
        {
            throw new ServiceUnavailableException("Authentication Service is not available");
        }
        this.strAccessToken = objToken.getAccessToken();
    }
    public void setGraph()
    {
        this.strGraph = GRAPH.concat(this.getTenantID());
    }
}

public class Connection1 extends Azure
{
    private static Connection1 objInstance;
    private Connection1() throws ParameterException, IOException, ParserConfigurationException, SAXException
    {
        super();
        this.setTenantID(<Tenant ID>);
        this.setLogin("https://login.microsoftonline.com/".concat(this.getTenantID()));
        this.setAuthorize(this.getLogin().concat("/oauth2/authorize"));
        this.setGraph();
        this.setApplicationID(<Application ID>);
        this.setAccessToken(null);
        this.setUsername(<username>);
        this.setPassword(<password>);
        this.setLogger();
    }
    public static Azure getInstance() throws ParameterException, IOException, ParserConfigurationException, SAXException
    {
        if (objInstance == null)
        {
            objInstance = new Connection1();
        }
        return objInstance;
    }
}

我有两个 类 Connection1 和 Connection2。 Connection2 是 Connection1 的副本,我唯一更改的是:

1) 租户 ID

2) 应用程序 ID

3) 用户名​​

4) 密码。

使用 Connection1,我可以毫无问题地验证和检索数据。 问题来自 Connection2,我收​​到以下错误:

[pool-3-thread-1] ERROR com.microsoft.aad.adal4j.AuthenticationContext - [Correlation ID: 63cc6344-2bc1-4f61-aaa0-a2f07acb172b] Execution of class com.microsoft.aad.adal4j.AcquireTokenCallable failed.
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

好像是证书错误,网上查了一下,建议加上"DigiCert Baltimore Root"证书到我的证书存储。证书已经在那里了。你知道我该如何面对吗?

根据你的报错信息,下面有两篇博客可以参考修复这个问题unable to find valid certification path to requested target

  1. https://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/
  2. http://nodsw.com/blog/leeland/2006/12/06-no-more-unable-find-valid-certification-path-requested-target

以上博客都使用了工具InstallCert来服务器证书,可以添加到本地密钥库。请遵循 GitHub 存储库的自述文件。

同时,这只是我的猜测,我认为一个可能的原因是 JVM 中证书存储的资源竞争。因此,如果您在 JVM 实例中 运行ning Connection1Connection2,您可以尝试在它们自己的独立 JVM 实例上分别 运行 它们,或者尝试复制 JAVA_HOME 目录并在命令行中设置临时 JAVA_HOME & PATH 环境变量 运行 另一个 Connection2 没有与他们共享任何资源。

真正找到问题所在。我使用了 Firefox 的 TamperData 插件并检查每个重定向以获取所有站点及其各自的证书。似乎这个特定租户发生了变化,而不是使用 DigiCert Baltimore Root,它结束于 Entrust.net Root