【问题标题】:Send .crt certificate in https request在 https 请求中发送 .crt 证书
【发布时间】:2019-05-18 12:12:32
【问题描述】:

我是证书概念的新手,任何人都可以帮助如何在 https 请求中发送证书。 我的 CURL 命令如下:

curl -d "grant_type=password&client_id=SmartRest&client_secret=594a27f3-4432-4d37-9196-2ba49de52758&username=user123&password=welcome123" https://xxxxxxx.xxx.in:8543/auth/realms/restapi/protocol/openid-connect/token --cacert ./ca_bundle.crt

我需要在我的 java 代码中发送同样的东西,我只有一个 .crt 文件,我没有 keypass 或任何东西。

【问题讨论】:

  • 为什么需要将 .crt 证书发送到 openid 端点?可能您需要它来验证服务器证书
  • @pedrofb - 客户端共享的 url,我只需要调用它。 curl 正在工作(主机更改)
  • --cacert 验证服务器证书。它不会将证书发送到服务器。要将 curl 转换为 Java,您需要使用密钥库(JKS 或 PKCS12)并将证书包含在其中。您可以在 SO 中找到许多示例
  • 您忘记提供链接

标签: java https certificate ssl-certificate


【解决方案1】:

我通过下面的 keytool 命令信任了证书。

keytool -import -trustcacerts -file "ca_bundle.crt" -alias "alias" -keystore  "C:\Program Files\Java\jdk1.8.0_131\jre\lib\security\cacerts"

以下代码运行良好,无需跳过 ssl 验证。

public static void main(String[] args) throws Exception {

   HttpsURLConnection con = (HttpsURLConnection) new URL("https://xxxx.xxx.xx:8543/auth/realms/restapi/protocol/openid-connect/token").openConnection();
   con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
   String urlParameters = "grant_type=password&client_id=SmartRest&client_secret=594a27f3-4432-4d37-9196-2ba49de52758&username=user123&password=welcome123";

   con.setDoOutput(true);
   DataOutputStream wr = new DataOutputStream(con.getOutputStream());
   wr.writeBytes(urlParameters);
   wr.flush();
   wr.close();

   BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
   String inputLine;
   StringBuffer response = new StringBuffer();

   while ((inputLine = in.readLine()) != null) {
      response.append(inputLine);
   }
   in.close();

   //print result
   System.out.println(response.toString());

}

我得到了结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多