All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.http.client.ClientHttpRequestFactoryCustom Maven / Gradle / Ivy

The newest version!
package org.springframework.http.client;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

/**
 * properties
 * 
 * @see org.springframework.core.task.SimpleAsyncTaskExecutor
 */
public class ClientHttpRequestFactoryCustom extends SimpleClientHttpRequestFactory {
  public static final String DEFAULT_PROTOCOL = "TLS";
  public Boolean followRedirects = false;
  public HostnameVerifier hostnameVerifier = createHostnameVerifier();
  public SSLContext sslContext = createSSLContext(DEFAULT_PROTOCOL);

  public void setFollowRedirects(Boolean followRedirects) {
    this.followRedirects = followRedirects;
  }

  public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
    this.hostnameVerifier = hostnameVerifier;
  }

  public void setSSLContext(SSLContext sslContext) {
    this.sslContext = sslContext;
  }

  @Override
  protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
    super.prepareConnection(connection, httpMethod);
    if (this.followRedirects != null) {
      connection.setInstanceFollowRedirects(this.followRedirects);
    }
    if (connection instanceof HttpsURLConnection) {
      if (this.sslContext != null && this.sslContext.getSocketFactory() != null) {
        ((HttpsURLConnection) connection).setSSLSocketFactory(this.sslContext.getSocketFactory());
      }
      if (this.hostnameVerifier != null) {
        ((HttpsURLConnection) connection).setHostnameVerifier(this.hostnameVerifier);
      }
    }
  }

  public static HostnameVerifier createHostnameVerifier() {
    return new SkipHostnameVerifier();
  }

  /**
   * @param protocol the standard name of the requested protocol. See the SSLContext
   * section in the Java
   * Cryptography Architecture Standard Algorithm Name Documentation for information
   * about standard protocol names. ({@code TLS}, {@code SSL}, etc.)
   * 
   * @see javax.net.ssl.SSLContext#getSocketFactory()
   * @see javax.net.ssl.SSLContext#getDefault()
   * @see javax.net.ssl.SSLContext#getDefaultSSLParameters()
   * @see javax.net.ssl.SSLParameters#getProtocols()
   */
  public static SSLContext createSSLContext(String protocol) {
    try {
      SSLContext sslContext = SSLContext.getInstance(protocol);
      sslContext.init(null, new TrustManager[] { new SkipX509TrustManager() }, new SecureRandom());
      return sslContext;
    }
    catch (GeneralSecurityException e) {
      throw new IllegalStateException("Create SSLContext fail... from " + protocol, e);
    }
  }
}

class SkipX509TrustManager implements X509TrustManager {
  @Override
  public X509Certificate[] getAcceptedIssuers() {
    return new X509Certificate[0];
  }

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

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

class SkipHostnameVerifier implements HostnameVerifier {
  @Override
  public boolean verify(String hostname, SSLSession session) {
    return true;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy