首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpsUrlConnection SSLHandshakeException certificate_required

HttpsUrlConnection SSLHandshakeException certificate_required
EN

Stack Overflow用户
提问于 2021-05-15 18:29:08
回答 3查看 674关注 0票数 1

我试图从java访问我的api,但是我得到了以下错误:

代码语言:javascript
复制
javax.net.ssl.SSLHandshakeException: Received fatal alert: certificate_required

在个人Windows计算机上测试代码时,不会收到此错误。但是这个错误确实发生在我使用openjdk 11运行Ubuntu的linux生产服务器上。

服务器托管在同一ubuntu服务器上,并使用Cloudflare SSL完全代理。

代码语言:javascript
复制
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服务器上没有正确配置一些东西?

更新:我的问题还没有解决,我仍在寻找答案。我在互联网上找不到任何信息丰富的博客或信息。

EN

回答 3

Stack Overflow用户

发布于 2022-02-07 21:53:41

服务器希望客户端提供它信任的证书,因此客户端需要配置一个Keystore --请参阅https://www.baeldung.com/java-https-client-certificate-authentication

票数 0
EN

Stack Overflow用户

发布于 2022-04-07 13:31:01

我遇到了类似的问题,同样的错误发生了,它帮助我(奇怪的是)在用户的根文件夹中复制证书--从启动命令的地方

/home/{user}/

票数 -1
EN

Stack Overflow用户

发布于 2022-07-19 13:13:35

尝试将服务器证书导入java信任库,并设置正确的SSLContext以使用信任库,然后尝试建立连接。从安全POV出发,推荐使用正确的证书和正确的信任库实现。

伪代码在下面

代码语言:javascript
复制
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.

有许多教程将帮助您开始使用这些。干杯。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67550047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档