简体   繁体   English

如何为 Https 请求配置 Web 客户端

[英]How to configure Web client for Https request

I am trying to understand how to configure the Web client.我试图了解如何配置 Web 客户端。 What I have is a working curl that I am not able to convert into a valid HTTPS request through (any) Java HTTP client. What I have is a working curl that I am not able to convert into a valid HTTPS request through (any) Java HTTP client. The curl is: curl 是:

curl -s --cert $CERTIFICATE --key $KEY https.url

where $CERTIFICATE is a .crt file containing:其中 $CERTIFICATE 是一个.crt文件,其中包含:

----BEGIN CERTIFICATE----
....
----END CERTIFICATE-----

And the $KEY is a .key file containing: $KEY 是一个.key文件,其中包含:

-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

I want to convert this curl into a valid JAVA request.我想将此 curl 转换为有效的 JAVA 请求。 Currently, I am configuring a Spring WebClient in this way:目前,我正在以这种方式配置 Spring WebClient:

private WebClient getWebClient() throws SSLException {
  SslContext sslContext = SslContextBuilder.forClient().keyManager(
                Paths.get(properties.getCrtFile()).toFile(),
                Paths.get(properties.getKeyFile()).toFile(),
                properties.getCertKeyPassword()).build();

  HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext));

  return WebClient
                .builder()
                .clientConnector(new ReactorClientHttpConnector(httpClient)).build();

}

But when I use the webclient to make a request it returns an error:但是当我使用 webclient 发出请求时,它返回一个错误:

exception: File does not contain valid private key: 

Any idea where is the error?知道错误在哪里吗?

This is how I solved the problem:这就是我解决问题的方法:

  1. Verify that .cert and .key files are valid:验证.cert.key文件是否有效:
openssl x509 -noout -modulus -in certFile.crt | openssl md5
#> (stdin)= 7f1a9c4d13aeaddd77fd4a0f241a6ce8

and

openssl rsa -noout -modulus -in certKey.key | openssl md5
#> (stdin)= 7f1a9c4d13aeaddd77fd4a0f241a6ce8
  1. Convert my .cert and .key files into a PCKS12 that Java can understand.将我的.cert.key文件转换成PCKS12可以理解的PCKS12。 (Keep in mind that my cert and key files are in PEM format as explained in the question). (请记住,我的证书和密钥文件采用 PEM 格式,如问题中所述)。 I used the following command:我使用了以下命令:
openssl pkcs12 -export -in certFile.crt -inkey keyFile.key -out cert.p12

This step will prompt you to enter a password.此步骤将提示您输入密码。 We will use this password when reading the certificate into a KeyStore.我们将在将证书读入 KeyStore 时使用此密码。

  1. Create an SSLContext by reading the certificate:通过读取证书创建 SSLContext:
private SslContext getSSLContext() {
  try (FileInputStream keyStoreFileInputStream = new 
    FileInputStream("pathTop12CertificateFile")) {
      KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
      keyStore.load(keyStoreFileInputStream,"password".toCharArray());
      KeyManagerFactory keyManagerFactory = 
              KeyManagerFactory.getInstance("SunX509");
      keyManagerFactory.init(keyStore, "password".toCharArray());

      return SslContextBuilder.forClient()
              .keyManager(keyManagerFactory)
              .build();

  } catch (Exception e) {
      log.error("An error has occurred: ", e);
  }
  return null;
}
  1. Build a Spring WebClient using this SSLContext:使用此 SSLContext 构建 Spring WebClient:
private WebClient getWebClient() {
    HttpClient httpClient = HttpClient.create().secure(sslSpec -> sslSpec.sslContext(getSSLContext()));
    ClientHttpConnector clientHttpConnector = new ReactorClientHttpConnector(httpClient);
    return WebClient
            .builder()
            .clientConnector(clientHttpConnector)
            .build();

}

Now we can use WebClient to make our HTTP Requests.现在我们可以使用 WebClient 来发出我们的 HTTP 请求。

From the details you provided, I understand that the SslContextBuilder can't understand your key type.根据您提供的详细信息,我了解到SslContextBuilder无法理解您的密钥类型。

Try to convert a non-encrypted key to PKCS8 using the following command.尝试使用以下命令将非加密密钥转换为 PKCS8。

openssl pkcs8 -topk8 -nocrypt -in pkcs1_key_file -out pkcs8_key.pem

Also, see the examples at https://netty.io/4.0/api/io/netty/handler/ssl/util/InsecureTrustManagerFactory.html that accepts without verification all X.509 certificates, including those that are self-signed.此外,请参阅https://netty.io/4.0/api/io/netty/handler/ssl/util/InsecureTrustManagerFactory.html中的示例,该示例无需验证即可接受所有 X.509 证书,包括那些自签名的证书。

Also discussed at https://groups.google.com/forum/#!topic/grpc-io/5uAK5c9rTHw也在https://groups.google.com/forum/#!topic/grpc-io/5uAK5c9rTHw 讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何跳过某些http请求的重定向到https? 如何在jboss Web子系统中配置它? - How to skip redirection to https for some http request? How to configure this in jboss web subsystem? 我如何在https和客户端证书请求后面生成Web服务代理 - how can i generate a web service proxy behind https and client certificate request Java 中的 HTTPS Web 请求 - HTTPS web request in Java 如何使用 https 配置 jhipster - how to configure jhipster with https 如何使用Java(Apache HTTP客户端)模拟浏览器HTTPS POST请求? - How to emulate a browser HTTPS POST request with Java (Apache HTTP Client)? 如何确定HTTP和HTTPS请求中客户端的公共IP? - How to determine public ip of client in HTTP and HTTPS request? 如何通过http客户端执行https get请求? - how to perform an https get request via http client? 如何在https地址的wsdl中生成Eclipse中的Web服务客户端? - How to generate a web service client in Eclipse from a wsdl at an https address? 如何为Java客户端应用程序配置简单的Web缓存? - How can I configure simple web cache for Java client application? org.springframework.web.client.ResourceAccessException:“https://[myendpoint]”的 GET 请求出现 I/O 错误 - org.springframework.web.client.ResourceAccessException: I/O error on GET request for 'https://[myendpoint]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM