2010-11-01 11 views
28

में एसएसएल प्रमाणपत्र प्रमाणीकरण को अक्षम करना मेरे पास दो अलग-अलग मशीनों पर दो स्प्रिंग आधारित वेब ऐप्स ए और बी हैं।वसंत RestTemplate

मैं वेब ऐप ए से वेब ऐप बी में एक https कॉल करना चाहता हूं, हालांकि मैं मशीन बी में एक स्व-हस्ताक्षरित प्रमाणपत्र का उपयोग कर रहा हूं, इसलिए मेरा HTTPS अनुरोध विफल हो गया है।

वसंत में RestTemplate का उपयोग करते समय मैं https प्रमाणपत्र सत्यापन को कैसे अक्षम कर सकता हूं? मैं सत्यापन अक्षम करने के लिए, क्योंकि दोनों वेब एप्लिकेशन ए और बी इंटरनेट नेटवर्क के भीतर कर रहे हैं चाहते हैं, लेकिन आंकड़ा अंतरण HTTPS पर होना होता है

उत्तर

26

क्या आप जोड़ने की जरूरत है एक कस्टम HostnameVerifier वर्ग प्रमाण पत्र सत्यापन बाईपास और सच रिटर्न है

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }); 

इसे आपके कोड में उचित रूप से रखा जाना चाहिए।

+0

मैं वसंत के रेस्ट टेम्पलेट वर्ग का उपयोग कर रहा हूं, क्या आप जानते हैं कि मैं RestTemplate में ऐसा कैसे कर सकता हूं? – Ram

+6

वास्तव में यह RestTemplate के बाहर है। Http://forum.springsource.org/showthread.php?t=60854 पर उपलब्ध कुछ नमूना कोड। Http://stackoverflow.com/questions/1725863/ssl-handshake-issue-using-spring-resttemplate – Raghuram

+0

पर भी देखें उन लिंक को देखा और इसे काम करने के लिए मिला। धन्यवाद :) – Ram

6
Add my response with cookie : 

    public static void main(String[] args) { 
      MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); 
      params.add("username", testUser); 
      params.add("password", testPass); 
NullHostnameVerifier verifier = new NullHostnameVerifier(); 
      MySimpleClientHttpRequestFactory requestFactory = new MySimpleClientHttpRequestFactory(verifier , rememberMeCookie); 
      ResponseEntity<String> response = restTemplate.postForEntity(appUrl + "/login", params, String.class); 

      HttpHeaders headers = response.getHeaders(); 
      String cookieResponse = headers.getFirst("Set-Cookie"); 
      String[] cookieParts = cookieResponse.split(";"); 
      rememberMeCookie = cookieParts[0]; 
      cookie.setCookie(rememberMeCookie); 

      requestFactory = new MySimpleClientHttpRequestFactory(verifier,cookie.getCookie()); 
      restTemplate.setRequestFactory(requestFactory); 
    } 


    public class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory { 

     private final HostnameVerifier verifier; 
     private final String cookie; 

     public MySimpleClientHttpRequestFactory(HostnameVerifier verifier ,String cookie) { 
      this.verifier = verifier; 
      this.cookie = cookie; 
     } 

     @Override 
     protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { 
      if (connection instanceof HttpsURLConnection) { 
       ((HttpsURLConnection) connection).setHostnameVerifier(verifier); 
       ((HttpsURLConnection) connection).setSSLSocketFactory(trustSelfSignedSSL().getSocketFactory()); 
       ((HttpsURLConnection) connection).setAllowUserInteraction(true); 
       String rememberMeCookie = cookie == null ? "" : cookie; 
       ((HttpsURLConnection) connection).setRequestProperty("Cookie", rememberMeCookie); 
      } 
      super.prepareConnection(connection, httpMethod); 
     } 

     public SSLContext trustSelfSignedSSL() { 
      try { 
       SSLContext ctx = SSLContext.getInstance("TLS"); 
       X509TrustManager tm = new X509TrustManager() { 

        public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { 
        } 

        public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { 
        } 

        public X509Certificate[] getAcceptedIssuers() { 
         return null; 
        } 
       }; 
       ctx.init(null, new TrustManager[] { tm }, null); 
       SSLContext.setDefault(ctx); 
       return ctx; 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
      return null; 
     } 

    } 


    public class NullHostnameVerifier implements HostnameVerifier { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     } 
+0

NullHostnameVerifier सत्यापनकर्ता = नया NullHostnameVerifier(); आप मुझे याद कर सकते हैं कि कुकी एक स्ट्रिंग –

19
@Bean 
public RestTemplate restTemplate() 
       throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { 
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; 

    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() 
        .loadTrustMaterial(null, acceptingTrustStrategy) 
        .build(); 

    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); 

    CloseableHttpClient httpClient = HttpClients.custom() 
        .setSSLSocketFactory(csf) 
        .build(); 

    HttpComponentsClientHttpRequestFactory requestFactory = 
        new HttpComponentsClientHttpRequestFactory(); 

    requestFactory.setHttpClient(httpClient); 
    RestTemplate restTemplate = new RestTemplate(requestFactory); 
    return restTemplate; 
} 
+0

आकर्षण .......... – Valath

9

अनिवार्य रूप से दो बातें आपको बस इतना करना है कि सभी प्रमाणपत्र पर भरोसा करता है एक कस्टम TrustStrategy उपयोग करते हैं, और यह भी NoopHostnameVerifier का उपयोग() होस्टनाम सत्यापन अक्षम करने हैं। यहां सभी प्रासंगिक आयातों के साथ कोड है:

import java.security.KeyManagementException; 
import java.security.KeyStoreException; 
import java.security.NoSuchAlgorithmException; 
import java.security.cert.CertificateException; 
import java.security.cert.X509Certificate; 
import javax.net.ssl.SSLContext; 
import org.apache.http.conn.ssl.NoopHostnameVerifier; 
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; 
import org.apache.http.conn.ssl.TrustStrategy; 
import org.apache.http.impl.client.CloseableHttpClient; 
import org.apache.http.impl.client.HttpClients; 
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; 
import org.springframework.web.client.RestTemplate; 

public RestTemplate getRestTemplate() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { 
    TrustStrategy acceptingTrustStrategy = new TrustStrategy() { 
     @Override 
     public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { 
      return true; 
     } 
    }; 
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build(); 
    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); 
    CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build(); 
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); 
    requestFactory.setHttpClient(httpClient); 
    RestTemplate restTemplate = new RestTemplate(requestFactory); 
    return restTemplate; 
} 
+1

धन्यवाद, यह मेरे लिए काम किया! – Humoyun

+1

ट्रस्टस्ट्रेटी स्वीकृति TrstStrategy = (X509 प्रमाण पत्र [] x509 प्रमाणपत्र, स्ट्रिंग एस) -> सत्य; – Humoyun

+1

धन्यवाद, यह मेरा जीवन बचाया। अन्य सभी विधियों (keytool का उपयोग कर जावा cacerts में निर्यात .crt जोड़ना, http://blog.codeleak.pl/2016/02/skip-ssl-certificate-verification-in.html, और लगभग 5 ~ 6 अन्य स्टैक ओवरफ्लो पोस्ट उत्तर सहित इस पर) असफल रहा, मेरे समय के 7 घंटे लगे। यह जवाब मेरी आखिरी उम्मीद थी, धन्यवाद। –

0

आप इसका उपयोग HTTPClient API के साथ कर सकते हैं।

public RestTemplate getRestTemplateBypassingHostNameVerifcation() { 
    CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); 
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); 
    requestFactory.setHttpClient(httpClient); 
    return new RestTemplate(requestFactory); 

} 
संबंधित मुद्दे