Клиент Dropwizard имеет дело с самозаверяющим сертификатом

#ssl #ssl-certificate #dropwizard #jersey-client

#ssl #ssl-сертификат #dropwizard #джерси-клиент

Вопрос:

Совершенно новое в Dropwizard.

Я нашел множество решений для работы с самозаверяющим сертификатом Jersey и ssl. Версия Dropwizard 0.9.2

Я попытался установить SSLContext, но я получаю

 The method sslContext(SSLContext) is undefined for the type JerseyClientBuilder
 

Код:

    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;
  }
 

Часть конфигурации:

 private JerseyClientConfiguration jerseyClient = new JerseyClientConfiguration();

public JerseyClientConfiguration getJerseyClient() {
    return jerseyClient;   
} 
 

Ответ №1:

Я нашел простое решение, просто используя конфигурацию

 jerseyClient:
  tls:
    verifyHostname: false
    trustSelfSignedCertificates: true
 

Комментарии:

1. Спасибо за ваш ответ.

Ответ №2:

Я думаю, что для создания небезопасного клиента в 0.9.2 вы бы использовали реестр ConnectionSocketFactory, что-то вроде…

     final SSLContext sslContext = SSLContext.getInstance("SSL");

    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s)
                    throws java.security.cert.CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        } }, new SecureRandom());


    final SSLConnectionSocketFactory sslConnectionSocketFactory =
            new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register("https", sslConnectionSocketFactory)
            .register("http", PlainConnectionSocketFactory.INSTANCE)
            .build();

    builder.using(registry);

    Client client = new JerseyClientBuilder(env)
      .using(configuration.getJerseyClient())
      .using(registry)
      .build("MyInsecureClient");