本文介绍了将SSLContext默认值替换为自己的实现和信任管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Jcs的答案()我试图更换 SSLContext.getDefault()与我自己的信任经理。

Based on this answer from Jcs (HttpUnit WebConversation SSL Issues) I tried to replace the SSLContext.getDefault() with my own trust manager.

SSLContext ssl = SSLContext.getDefault();
ssl.init(null, new X509TrustManager[]{new AnyTrustManager()}, null);
ssl.setDefault(ssl);

AnyTrustManager():

AnyTrustManager():

import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;

public class AnyTrustManager implements X509TrustManager
{
  X509Certificate[] client = null;
  X509Certificate[] server = null;

  public void checkClientTrusted(X509Certificate[] chain, String authType)
  {
    client = chain;
  }

  public void checkServerTrusted(X509Certificate[] chain, String authType)
  {
    server = chain;
  }

  public X509Certificate[] getAcceptedIssuers()
  {
    return new X509Certificate[0];
  }
}

我需要这样做,因为第三方.jar仅使用SSLContext默认值导致我出现一些问题,因此在此操作期间我必须将默认值更改为其他内容并稍后将其更改回来。

I need to do this because a 3rd party .jar is only using the SSLContext default which causes me some issues so for the duration of this action I have to change the default to something else and change it back later.

这将是不幸的是抛出一个 java.security.KeyManagementException:默认SSLContext自动初始化异常。

This will unfortunately throw a java.security.KeyManagementException: Default SSLContext is initialized automatically exception.

如何让它在Java 8上运行?

How can I get this to work on Java 8?

推荐答案

默认SSLContext是不可变的。因此,TrustManager实例是不可能的。相反,你应该替换

The "default" SSLContext is immutable. Therefore it is not possible the TrustManager instance. Instead you should replace

SSLContext ssl = SSLContext.getDefault();

by(例如)

SSLContext ssl = SSLContext.getInstance("TLSv1");

这篇关于将SSLContext默认值替换为自己的实现和信任管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:34