gpt4 book ai didi

java - 如何让 Jersey 客户端使用 HttpsURLConnection 中设置的 defaultSSLSocketFactory?

转载 作者:太空宇宙 更新时间:2023-11-03 14:07:25 26 4
gpt4 key购买 nike

我有一个项目,其中 HttpsURLConnection 配置为使用自定义的 TrustManager,如下所示:

SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());

本项目中有一个REST API客户端,它使用Jersey客户端发送HTTP/HTTPS请求:

Client client = ClientBuilder.newClient();

但是,这个 Jerset 客户端发起的 HTTPS 连接没有使用我在 HttpsURLConnection 中设置的 defaultSSLSocketFactory,它无法连接到不受信任的 HTTPS url。

我需要在此客户端上显式设置 SslContext 以使其与我的 TrustManager 一起工作。

SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null);
Client client = ClientBuilder.newBuilder().sslContext(sslContext).build();

有什么办法可以解决这个问题吗?

谢谢。

最佳答案

我最终找到的解决方案是将 SSLSocketFactory 提供程序属性设置为自定义的 SSLSocketFactory。希望这可以帮助其他有类似问题的人。

在程序开头调用:

Security.setProperty("ssl.SocketFactory.provider", MySSLSocketFactory.class.getCanonicalName());

这是 MySSLSocketFactory 的样子(它还设置了连接超时):

public class MySSLSocketFactory extends SSLSocketFactory {

private SSLContext sslContext = SSLContext.getInstance(Const.Ssl.PROTOCOL_SSL);


public MySSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException {
this.sslContext.init(
null,
new TrustManager[] { new MyTrustManager(false) },
new SecureRandom());
}


@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
throws IOException {
socket.connect(new InetSocketAddress(host, port), Const.Ssl.CONNECT_TIMEOUT);
socket.setSoTimeout(Const.Ssl.DATA_TIMEOUT);
return this.sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}


@Override
public String[] getDefaultCipherSuites() {
return this.sslContext.getSocketFactory().getDefaultCipherSuites();
}


@Override
public String[] getSupportedCipherSuites() {
return this.sslContext.getSocketFactory().getSupportedCipherSuites();
}


@Override
public Socket createSocket(String host, int port)
throws IOException, UnknownHostException {
return this.createSocket(new Socket(), host, port, true);
}


@Override
public Socket createSocket(InetAddress address, int port)
throws IOException {
return this.createSocket(new Socket(), address.getHostAddress(), port, true);
}


@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
throws IOException, UnknownHostException {
return this.createSocket(new Socket(), host, port, true);
}


@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
throws IOException {
return this.createSocket(new Socket(), address.getHostAddress(), port, true);
}

关于java - 如何让 Jersey 客户端使用 HttpsURLConnection 中设置的 defaultSSLSocketFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38535821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com