Spring BootのRestTemplateのHttpURLConnectionをSSL通信にカスタマイズする方法

Spring BootのRestTemplateのHttpURLConnectionをSSL通信にカスタマイズする方法

RestTemplateで標準で使用されているSimpleClientHttpRequestFactoryクラスですが、このクラスのHttpURLConnectionをカスタマイズしてhttpsのRest APIを実行できるようにします。

RestTemplateでSSL通信したい場合にこの実装をしました。pfxファイルはS3に配置している前提です。

RestClientConfig.java

package jp.co.confrage.config;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Component
public class RestClientConfig {
  private final AmazonS3 s3;
  private final S3Config s3Config;

  @Bean
  public RestTemplate restTemplate() throws Exception {
    KeyManagerFactory keyManagerFactory;
    try (InputStream inputStream = getS3Pfx()) {
      KeyStore keyStore = KeyStore.getInstance("PKCS12");
      String password = "password";
      keyStore.load(inputStream, password.toCharArray());
      keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
      keyManagerFactory.init(keyStore, password.toCharArray());
      SSLContext sslContext = SSLContext.getInstance("TLS");
      sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
      HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    } catch (KeyStoreException
      | NoSuchAlgorithmException
      | CertificateException
      | IOException
      | UnrecoverableKeyException
      | KeyManagementException e) {
      e.printStackTrace();
    }
    return new RestTemplate();
  }
  private InputStream getS3Pfx() throws Exception {
    String bucket = s3Config.getBucket(); // testbucket バケット名
    String key = s3Config.getObjectKey(); // etc/keys/hoge.pfx pfxファイル配置場所
    S3Object object = s3.getObject(new GetObjectRequest(bucket, key));
    return object.getObjectContent();
  }
}

SimpleClientHttpRequestFactoryクラスを継承したクラスを作成してRestTemplateに設定する必要があると思ったのですが、HttpsURLConnection.setDefaultSSLSocketFactoryメソッドがstaticメソッドなので、このBean登録しておくだけでHTTPS通信が可能なRestTemplateクラスになります。

Spring BootでRestTemplateのClientHttpRequestFactory実装クラスをHttpComponentsClientHttpRequestFactoryに変更する

参考サイト:https://www.it-swarm.dev/ja/spring/spring-resttemplate%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6https-rest%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9%E3%81%AB%E3%82%A2%E3%82%AF%E3%82%BB%E3%82%B9%E3%81%99%E3%82%8B/1041272463/

コメント

タイトルとURLをコピーしました