gpt4 book ai didi

java - 忽略 SSL 验证 HttpClient/Rest API - Groovy

转载 作者:行者123 更新时间:2023-12-04 22:37:00 40 4
gpt4 key购买 nike

我正在尝试使用 Groovy 使用 Rest API,这是我正在使用的代码:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

CloseableHttpClient client = HttpClients.createDefault();
HttpResponse response = null;

HttpPost request = new HttpPost("http://path_to_my_server/rest_api_path");
String auth = "login:password";
String encoding = auth.bytes.encodeBase64().toString()
request.setHeader("Authorization", "Basic " + encoding);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("postVar", new StringBody("value",ContentType.MULTIPART_FORM_DATA));
HttpEntity entity = builder.build();
request.setEntity(entity);
response = client.execute(request);
问题是每次我执行它时,它都会向我显示一个错误:

error javax.net.ssl.SSLHandshakeException: PKIX path building failed:sun.security.provider.certpath.SunCertPathBuilderException: unable tofind valid certification path to requested target caused by:sun.security.validator.ValidatorException: PKIX path building failed:sun.security.provider.certpath.SunCertPathBuilderException: unable tofind valid certification path to requested target caused by:sun.security.provider.certpath.SunCertPathBuilderException: unable tofind valid certification path to requested target


我做了一些研究,并测试了一些代码,例如:
https://gist.github.com/barata0/63705c0bcdd1054af2405e90c06f6b71
https://github.com/jgritman/httpbuilder/wiki/SSL
How to use SSL with a self-signed certificate in groovy?
所有这些都不起作用,请帮助?

最佳答案

尽管我不建议您使用此选项 不要忽略ssl验证 .更好的方法是创建一个包含受信任证书的 truststore.jks 并将其加载到您的 http 客户端中。
但是,您可能正在尝试做的事情。您可以做的类似于这里的答案:Disabling SSL verification for Elastic search Restclient not working in Java
我建议使用 X509ExtenderTrustManager而不是 X509TrustManager因为它在这些用例中可能表现不同。
您可以尝试以下代码段:
创建自定义 TrustManager

import javax.net.ssl.SSLEngine;
import javax.net.ssl.X509ExtendedTrustManager;
import java.net.Socket;
import java.security.cert.X509Certificate;

public final class UnsafeX509ExtendedTrustManager extends X509ExtendedTrustManager {

private static final X509ExtendedTrustManager INSTANCE = new UnsafeX509ExtendedTrustManager();
private static final X509Certificate[] EMPTY_CERTIFICATES = new X509Certificate[0];

private UnsafeX509ExtendedTrustManager() {}

public static X509ExtendedTrustManager getInstance() {
return INSTANCE;
}

@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType) {}

@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType, Socket socket) {}

@Override
public void checkClientTrusted(X509Certificate[] certificates, String authType, SSLEngine sslEngine) {}

@Override
public void checkServerTrusted(X509Certificate[] certificates, String authType) {}

@Override
public void checkServerTrusted(X509Certificate[] certificates, String authType, Socket socket) {}

@Override
public void checkServerTrusted(X509Certificate[] certificates, String authType, SSLEngine sslEngine) {}

@Override
public X509Certificate[] getAcceptedIssuers() {
return EMPTY_CERTIFICATES;
}

}
将其应用到您的 Http 客户端
HostnameVerifier hostnameVerifier = (host, sslSession) -> true;
TrustManager[] trustManagers = new TrustManager[]{UnsafeX509ExtendedTrustManager.getInstance()};

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, null);

LayeredConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(socketFactory)
.build();
请参阅我对其他 stackoverflow 的引用以获得更详细的说明: https://stackoverflow.com/a/64982379/6777695

关于java - 忽略 SSL 验证 HttpClient/Rest API - Groovy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67029215/

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