我试图从java访问我的api,但是我得到了以下错误:
javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_required在个人Windows计算机上测试代码时,不会收到此错误。但是这个错误确实发生在我使用openjdk 11运行Ubuntu的linux生产服务器上。
服务器托管在同一ubuntu服务器上,并使用Cloudflare SSL完全代理。
HttpsURLConnection con = null;
try {
URL url = new URL("https://www.example.com/");
con = (HttpsURLConnection) url.openConnection();
con.addRequestProperty("XF-Api-Key", "key");
con.addRequestProperty("XF-Api-User", "1");
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
con.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "application/json");
try {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null)
content.append(inputLine);
in.close();
System.out.println(content.toString());
} catch (Exception e) {
e.printStackTrace();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
String inputLine;
StringBuffer content = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println(content.toString());
}
} catch (Exception exp) {
System.out.println("Cant load data from API");
exp.printStackTrace();
} finally {
if (con != null)
con.disconnect();
}是不是在我的linux服务器上没有正确配置一些东西?
更新:我的问题还没有解决,我仍在寻找答案。我在互联网上找不到任何信息丰富的博客或信息。
发布于 2022-02-07 21:53:41
服务器希望客户端提供它信任的证书,因此客户端需要配置一个Keystore --请参阅https://www.baeldung.com/java-https-client-certificate-authentication。
发布于 2022-04-07 13:31:01
我遇到了类似的问题,同样的错误发生了,它帮助我(奇怪的是)在用户的根文件夹中复制证书--从启动命令的地方
/home/{user}/
发布于 2022-07-19 13:13:35
尝试将服务器证书导入java信任库,并设置正确的SSLContext以使用信任库,然后尝试建立连接。从安全POV出发,推荐使用正确的证书和正确的信任库实现。
伪代码在下面
1 - Load truststore
2 - Import certificate // Optional if you imported certificate directly using keytool in jdk
3 - Initialize SSLContext with the truststore already created
4 - Implement hostnameverifier if needed // only if needed
5 - Connect to endpoint.有许多教程将帮助您开始使用这些。干杯。
https://stackoverflow.com/questions/67550047
复制相似问题