Dropwizard 客户端处理自签名证书

Ôre*_*rel 2 ssl ssl-certificate dropwizard jersey-client

Dropwizard 相当新。

我找到了很多处理 Jersey 和 ssl 自签名证书的解决方案。Dropwizard 版本是 0.9.2

我试图设置 SSLContext 但我得到了

The method sslContext(SSLContext) is undefined for the type JerseyClientBuilder
Run Code Online (Sandbox Code Playgroud)

代码:

   TrustManager[] certs = new TrustManager[]{
          new X509TrustManager() {
              @Override
              public X509Certificate[] getAcceptedIssuers() {
                  return null;
              }

              @Override
              public void checkServerTrusted(X509Certificate[] chain, String authType)
                      throws CertificateException {
              }

              @Override
              public void checkClientTrusted(X509Certificate[] chain, String authType)
                      throws CertificateException {
              }
          }
  };

  public static class TrustAllHostNameVerifier implements HostnameVerifier {

      public boolean verify(String hostname, SSLSession session) {
          return true;
      }

  }
  private Client getWebClient(AppConfiguration configuration, Environment env) {
      SSLContext ctx = SSLContext.getInstance("SSL");
      ctx.init(null, certs, new SecureRandom());
      Client client = new JerseyClientBuilder(env)
          .using(configuration.getJerseyClient())
          .sslContext(ctx)
          .build("MyClient");
      return client;
  }
Run Code Online (Sandbox Code Playgroud)

配置部分:

private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

public JerseyClientConfiguration getJerseyClient() {
    return jerseyClient;   
} 
Run Code Online (Sandbox Code Playgroud)

Ôre*_*rel 7

我找到了一个简单的解决方案,只需使用配置

jerseyClient:
  tls:
    verifyHostname: false
    trustSelfSignedCertificates: true
Run Code Online (Sandbox Code Playgroud)