Azure SDK for Java – 如何忽略 SSL 错误

huangapple 未分类评论63阅读模式
标题翻译

Azure SDK for Java - How to ignore SSL errors

问题

我一直在尝试使用Azure SDK从我的Azure账户中获取一些信息。

我需要禁用SSL证书检查。

public static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[]{
    new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
        }
    }
};

private SSLSocketFactory sslFactory() {
    try {
        final SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, UNQUESTIONING_TRUST_MANAGER, null);
        return sc.getSocketFactory();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

AzureTokenCredentials credentials = new ApplicationTokenCredentials(
    "XXX",
    "XXX",
    "XXX",
    AzureEnvironment.AZURE
).withSslSocketFactory(sslFactory());

Azure azure = Azure.configure().authenticate(credentials).withSubscription("XXX");
azure.resourceGroups().list()

即使使用了这个SSLSocketFactory,我仍然遇到了以下错误:

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
	at sun.security.validator.Validator.validate(Validator.java:262)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)

我在 sun.security.ssl.ClientHandshaker.class:1008 进行了调试:
((X509ExtendedTrustManager)var6).checkServerTrusted((X509Certificate[])var2.clone(), var4, this.conn);

这一行会执行两次。第一次,var6拥有我的SSL工厂,第二次,它是一个不同的工厂,抛出了异常。

如何禁用SSL检查?

英文翻译

I have been trying to use the Azure SDK to fetch some information from an Azure account I have.

I need to disable ssl certificate checking.

 public static final TrustManager[] UNQUESTIONING_TRUST_MANAGER = new TrustManager[]{
            new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
    };

private SSLSocketFactory sslFactory() {
    try {
        final SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, UNQUESTIONING_TRUST_MANAGER, null);
        return sc.getSocketFactory();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

 AzureTokenCredentials credentials = new ApplicationTokenCredentials(
     "XXX",
     "XXX",
     "XXX",
     AzureEnvironment.AZURE
 ).withSslSocketFactory(sslFactory());

Azure azure = Azure.configure().authenticate(credentials).withSubscription("XXX");
azure.resourceGroups().list()

Even with that SSLSocketFactory I get:

Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
	at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:397)
	at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:302)
	at sun.security.validator.Validator.validate(Validator.java:262)
	at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
	at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
	at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
	at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1621)

I debugged sun.security.ssl.ClientHandshaker.class:1008:
((X509ExtendedTrustManager)var6).checkServerTrusted((X509Certificate[])var2.clone(), var4, this.conn);

It passes twice in this line. The first time, var6 has my ssl factory, the second time, it's a different factory that throw that exception.

How can I disable the SSL checking?

huangapple
  • 本文由 发表于 2020年3月4日 04:35:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/60515100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定