Azure连接无法连接证书错误

时间:2019-02-01 18:29:28

标签: java azure adal4j

我很难根据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” 证书添加到我的证书存储中。证书已经在那里。你有什么想法我应该面对吗?

2 个答案:

答案 0 :(得分:1)

实际找到问题所在。我使用了Firefox的TamperData addIn,并检查每个重定向以获取所有带有各自证书的站点。似乎此特定租户发生了变化,而不是使用DigiCert Baltimore Root,而是以Entrust.net Root结尾

答案 1 :(得分:0)

仅根据您的错误信息,您可以参考下面的两个博客来解决此问题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实例中运行Connection1Connection2,则可以尝试分别在它们自己的独立JVM实例上运行它们,或者尝试复制JAVA_HOME目录并设置一个临时目录。在命令行中使用JAVA_HOMEPATH环境变量来运行其他Connection2,而无需与它们共享任何资源。