2017-10-01 31 views
0

私はSpringブートアプリケーションでSSLセキュリティを使用しています。アドレスRestTemplateとSSLの併用

final UriComponents uriComponents 
      = uriComponentsBuilder.path("/api/v1.0/register/token/{token}").buildAndExpand(token); 

    ResponseEntity<Boolean> response; 

    try { 
     response = restTemplate 
       .exchange(uriComponents.toUri(), 
          HttpMethod.PUT, 
          entity, 
          Boolean.class); 

スローを呼び出している間離れて、私は慎重に

I/O error on PUT request for "https://localhost:8443/api/v1.0/register/token/PBe3AzJ245W0sNyeg": java.security.cert.CertificateException: No name matching localhost found; nested exception is javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found 

私はそれを追加した後、ウェブサイトhttp://java.globinch.com/enterprise-java/security/fix-java-security-certificate-exception-no-matching-localhost-found/

static { 
    //for localhost testing only 
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
      (hostname, sslSession) -> hostname.equals("localhost")); 
    } 

にインターネット上で見つけhttps://pastebin.com/A4Vb69hT、それはhttps://pastebin.com/kJZCqJ6K慎重

さらにエラーを受け取ります

I/O error on PUT request for "https://localhost:8443/api/v1.0/register/token/EMNy7W9jJgsMWEn0z6hFOIHoB96zzSaeHWUs": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

今すぐどうすればよいですか?

私は、サーバーの証明書が有効な証明書チェーンではないか、おそらく自己署名されている2つのSSLファイルhttps://github.com/JonkiPro/REST-Web-Services/tree/master/src/main/resources/keystore

+0

を呼び出します。方法については、[この質問](https://stackoverflow.com/questions/30121510/java-httpsurlconnection-and-tls-1-2)を参照してください。 –

答えて

1

この答えは、実際の環境で有効にすると、まだ証明書で動作するでしょうがローカルで実行するにはハックのいくつかの種類です。

ちょうどあなたが `HttpURLConnection`を使用したいとあなたが最初のSSLContextを定義する必要がSSLContextHelper.disable()

restTemplate.exchange
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.SecureRandom; 
import java.security.UnrecoverableKeyException; 
import java.security.cert.CertificateException; 

import javax.net.ssl.HostnameVerifier; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.KeyManager; 
import javax.net.ssl.KeyManagerFactory; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.SSLSession; 
import javax.net.ssl.TrustManager; 
import javax.net.ssl.TrustManagerFactory; 
import javax.net.ssl.X509TrustManager; 
import javax.security.cert.X509Certificate; 

import org.apache.log4j.Logger; 

public class SSLContextHelper { 

    private static final String KEY_STORE_TYPE="JKS"; 
    private static final String CLASS_NAME=SSLContextHelper.class.getName(); 
    private static final String TRANSPORT_SECURITY_PROTOCOL="TLS"; 
    private static final Logger logger=Logger.getLogger(SSLContextHelper.class); 

    public static void enable(){ 
     String keystoreType = "JKS"; 
     InputStream keystoreLocation = null; 
     char [] keystorePassword = null; 
     char [] keyPassword = null; 


     try { 
       KeyStore keystore = KeyStore.getInstance(keystoreType);  
       keystore.load(keystoreLocation, keystorePassword); 
       KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
       kmfactory.init(keystore, keyPassword); 
       InputStream truststoreLocation = null; 
       char [] truststorePassword = null; 
       String truststoreType = KEY_STORE_TYPE; 

       KeyStore truststore = KeyStore.getInstance(truststoreType); 
       truststore.load(truststoreLocation, truststorePassword); 
       TrustManagerFactory tmfactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); 

       KeyManager [] keymanagers = kmfactory.getKeyManagers(); 
      TrustManager [] trustmanagers = tmfactory.getTrustManagers(); 

      SSLContext sslContext = SSLContext.getInstance(TRANSPORT_SECURITY_PROTOCOL); 
      sslContext.init(keymanagers, trustmanagers, new SecureRandom()); 
      SSLContext.setDefault(sslContext); 
     } catch (Exception e) { 
      logger.error(CLASS_NAME+"Exception in SSL "+e.getMessage()); 
      e.printStackTrace(); 
     } 
    } 

    public static void disable() { 
     try { 
      SSLContext sslc = SSLContext.getInstance("TLS"); 
      TrustManager[] trustManagerArray = { (TrustManager) new NullX509TrustManager() }; 
      sslc.init(null, trustManagerArray, null); 
      HttpsURLConnection.setDefaultSSLSocketFactory(sslc.getSocketFactory()); 
      HttpsURLConnection.setDefaultHostnameVerifier(new NullHostnameVerifier()); 
     } catch(Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    private static class NullX509TrustManager implements X509TrustManager { 
     public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      System.out.println(); 
     } 
     public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      System.out.println(); 
     } 

    } 

    private static class NullHostnameVerifier implements HostnameVerifier { 
     public boolean verify(String hostname, SSLSession session) { 
      return true; 
     } 
    } 
関連する問題