【问题标题】:SSLHandshakeException - Chain chain validation failed, how to solve?SSLHandshakeException - 链链验证失败,如何解决?
【发布时间】:2019-04-15 08:31:41
【问题描述】:

在我的应用程序中,我正在尝试向我的服务器发出 HTTPS POST 请求。 但是,我一直收到 SSLHandshakeException - 链验证失败,一直。我尝试使用 POSTMAN 发送请求,但我得到了服务器的响应。当我尝试从应用程序发送请求时,可能导致此错误的原因是什么?

这是我尝试发送帖子请求的代码 sn-p:

   public static JSONObject getDataLibConfiguration(Context context) throws HttpRequestException {

    int statusCode = 0;

    JSONObject commonInformation;
    HttpsURLConnection connection = null;

    try {

        commonInformation = ConfigurationProcessor.getCommonInformation(context);
        if (commonInformation == null) {
            return null;
        }

        URL url = new URL(BuildConfig.SERVER_CONFIG_URL);
        if (BuildConfig.DEBUG) {
            LogUtils.d(TAG, "url = " + url.getPath());
        }

        connection = getHttpsConnection(url);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        connection.setRequestProperty("Content-Encoding", "gzip");

        byte[] gzipped = HttpUtils.gzip(commonInformation.toString());
        cos = new CountingOutputStream(connection.getOutputStream()); //<-- This is where I get the exception
        cos.write(gzipped);
        cos.flush();

        statusCode = connection.getResponseCode();
        // More code her
 }

private static HttpsURLConnection getHttpsConnection(URL url) throws IOException, GeneralSecurityException {

        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();

        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            MatchDomainTrustManager myTrustManager = new MatchDomainTrustManager(url.getHost());
            TrustManager[] tms = new TrustManager[]{myTrustManager};
            sslContext.init(null, tms, null);
            SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            connection.setSSLSocketFactory(sslSocketFactory);
        } catch (AssertionError ex) {
            if (BuildConfig.DEBUG) {
                LogFileUtils.e(TAG, "Exception in getHttpsConnection: " + ex.getMessage());
            }
            LogUtils.e(TAG, "Exception: " + ex.toString());
        }
        return connection;
    }

【问题讨论】:

  • LogCat 中的错误是什么?
  • 它只是说:“java.security.cert.CertificateException:链验证失败”

标签: android https sslhandshakeexception


【解决方案1】:

问题是证书已过期。

【讨论】:

  • 但就我而言,它几乎可以在所有设备上运行,但很少有人收到此错误消息
  • 哪个证书?如何续订?
  • @ahmadalibaloch 我发送请求的服务器有一个过期的证书,所以我不得不在 GoDaddy 上更新它。
  • 就我而言,我的模拟器日期未更新。晚了几个星期
【解决方案2】:
public static void trustEveryone() {
        try {
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }});
            SSLContext context = SSLContext.getInstance("TLS");
            context.init(null, new X509TrustManager[]{new X509TrustManager(){
                public void checkClientTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public void checkServerTrusted(X509Certificate[] chain,
                                               String authType) throws CertificateException {}
                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }}}, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(
                    context.getSocketFactory());
        } catch (Exception e) { // should never happen
            e.printStackTrace();
        }
    }

或检查您设备的系统日期 - 当我尝试连接错误的日期时出现此异常!..

【讨论】:

  • 我尝试使用此类代码发布更新,但出于安全原因,Google 阻止了发布。
【解决方案3】:

在我的情况下,电话上的日期是错误的。

修复日期解决了问题

【讨论】:

  • 我在安卓电视模拟器上遇到了同样的问题。更新日期和设置正确的时区解决了这个问题。
  • 谢谢哥们。拯救我的一天。 Glide 产生了这个问题
  • 这就是我的解决方案。我只是将日期设置为“自动”并且它有效。但是有没有办法让它与“手动”日期设置一起工作?
  • @Krishnom,如果您手动设置正确的时间应该可以工作
  • 这对我有用。谢谢:)
【解决方案4】:

我只是通过将 api 的基本 url 从“https”更改为“http”来修复它

【讨论】:

  • 这是一种非常糟糕的解决方法。您不需要使用不安全的协议来解决 SSL 握手问题。
  • 无法在某些自动重定向到 https 的网站上使用该技巧。
【解决方案5】:

如果有人遇到与特定设备有关的此问题,则原因应该是设备中设置的日期不正确。

【讨论】:

    【解决方案6】:

    如果您使用的是模拟设备,只需“冷启动”即可解决问题。

    如果您让它们运行一段时间,有时这些东西的日期可能会卡住,从而导致这个过期证书问题。

    【讨论】:

    • 这对我有用。谢谢:)
    【解决方案7】:

    我的日期和时间是正确的,但我的系统设置中没有“使用网络提供的时间检查”。

    我通过转到“设置”>“日期和时间”>选中“使用网络提供的时间”并选中“使用网络提供的时区”来解决此问题。

    然后这个错误就消失了。

    【讨论】:

    • 在 OnePlus 8 上,两者都被检查,但仍然发生错误。
    【解决方案8】:

    @Yash Bhardwaj 在@Vadim 答案的评论中说问题出在 Glide 框架中。我遇到了同样的问题:使用 Ktor 框架对服务器的 Https 请求都成功了,但是当 Glide 尝试从同一服务器加载图像时,它遇到了SSLHandshakeException。 要解决这个问题,你应该看这里:Solve Glide SSLHandshakeException

    要处理@GlideModule 注解,您应该导入kapt 插件并将这些依赖项添加到您的应用程序build.gradle

    implementation 'com.github.bumptech.glide:okhttp3-integration:4.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.12.0'
    

    【讨论】:

      【解决方案9】:

      我通过重置我的模拟器日期和时间来修复此错误。我的服务器工作正常,只是我将模拟器的日期和时间更改为当前服务器时区。

      【讨论】:

        【解决方案10】:

        就我而言,我在 Android Emulator 上获取了这个问题。 当我清除模拟器缓存时,问题已解决。

        【讨论】:

          猜你喜欢
          • 2019-01-30
          • 2019-09-15
          • 1970-01-01
          • 2011-08-08
          • 2016-10-10
          • 1970-01-01
          • 2021-04-11
          • 2019-12-06
          相关资源
          最近更新 更多