2010-07-30 10 views
12

का उपयोग कर x509 सर्टिफिकेट्स के साथ म्यूचुअल प्रमाणीकरण क्या किसी के पास HTTP क्लाइंट 4.0.1 का उपयोग कर x509 प्रमाणपत्र के माध्यम से क्लाइंट प्रमाणीकरण करने के तरीके पर कोई दोस्ताना सुझाव है?एचटीपी क्लाइंट 4.0.1

आपके समय के लिए धन्यवाद।

उत्तर

21

यहां जाने के लिए कुछ कोड यहां दिए गए हैं। KeyStore वह ऑब्जेक्ट है जिसमें क्लाइंट प्रमाणपत्र शामिल है। यदि सर्वर एक स्व-हस्ताक्षरित प्रमाणपत्र या प्रमाण पत्र का उपयोग कर रहा है जो किसी सीए द्वारा हस्ताक्षरित नहीं है जैसा कि शामिल कैकर्ट फ़ाइल में JVM द्वारा मान्यता प्राप्त है तो आपको TrustStore का उपयोग करने की आवश्यकता होगी। अन्यथा डिफ़ॉल्ट cacerts फ़ाइल का उपयोग करने, .. truststore तर्क के लिए SSLSockeFactory को null में पारित

import org.apache.http.conn.scheme.Scheme; 
import org.apache.http.conn.scheme.SchemeRegistry; 
import org.apache.http.conn.ssl.SSLSocketFactory; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; 
import org.apache.http.params.BasicHttpParams; 
import org.apache.http.params.HttpParams; 

... 

final HttpParams httpParams = new BasicHttpParams(); 

// load the keystore containing the client certificate - keystore type is probably jks or pkcs12 
final KeyStore keystore = KeyStore.getInstance("pkcs12"); 
InputStream keystoreInput = null; 
// TODO get the keystore as an InputStream from somewhere 
keystore.load(keystoreInput, "keystorepassword".toCharArray()); 

// load the trustore, leave it null to rely on cacerts distributed with the JVM - truststore type is probably jks or pkcs12 
KeyStore truststore = KeyStore.getInstance("pkcs12"); 
InputStream truststoreInput = null; 
// TODO get the trustore as an InputStream from somewhere 
truststore.load(truststoreInput, "truststorepassword".toCharArray()); 

final SchemeRegistry schemeRegistry = new SchemeRegistry(); 
schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keystore, keystorePassword, truststore), 443)); 

final DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams); 
+0

धन्यवाद। मैं जल्द ही इसे देख लूंगा। – hooknc

+0

यह समाधान पूरी तरह से काम किया। तुम्हारे सहयोग के लिए तुम्हे धन्यवाद। FYI करें, कारण एसएसएल सहभागिता रीनिगोशिएशन मुद्दों के लिए मैं निम्नलिखित आभासी मशीन गुण सेट करना पड़ा: -Dsun.security.ssl.allowUnsafeRenegotiation = सच मैं/जावा 1.6.0_20 और बिल्ला 6.0.29 के साथ काम कर रहा था हूँ। – hooknc

+2

jdk 1.6.0_24 या बाद में काम करते समय उपर्युक्त टिप्पणी की आवश्यकता नहीं है। – hooknc

2

एक अन्य समाधान (एक और उदाहरण से नकल)। मैंने दोनों 'ट्रस्टिंग' (ट्रस्टस्टोर) और खुद को प्रमाणित करने के लिए एक ही कीस्टोर का उपयोग किया है (कीस्टोर)।

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
FileInputStream instream = new FileInputStream(new File("miller.keystore")); 
try { 
    trustStore.load(instream, "pw".toCharArray()); 
} finally { 
    instream.close(); 
} 

SSLContext sslcontext = SSLContexts.custom() 
     .loadTrustMaterial(trustStore) /* this key store must contain the certs needed & trusted to verify the servers cert */ 
     .loadKeyMaterial(trustStore, "pw".toCharArray()) /* this keystore must contain the key/cert of the client */ 
     .build(); 

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, 
     SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); 
CloseableHttpClient httpclient = HttpClients.custom() 
     .setSSLSocketFactory(sslsf) 
     .build(); 
try { 

    HttpGet httpget = new HttpGet("https://localhost"); 

    System.out.println("executing request" + httpget.getRequestLine()); 

    CloseableHttpResponse response = httpclient.execute(httpget); 
    try { 
     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     if (entity != null) { 
      System.out.println("Response content length: " + entity.getContentLength()); 
     } 
     EntityUtils.consume(entity); 
    } finally { 
     response.close(); 
    } 
} finally { 
    httpclient.close(); 
} 
0

मैंने निम्नलिखित एचटीपी क्लाइंट की वेबसाइट पर नमूना कोड से निम्नलिखित का उपयोग किया (कस्टम एसएसएल संदर्भ अगर मुझे सही याद है)।

{ 
    KeyStore keyStore = KeyStore.getInstance("PKCS12"); //client certificate holder 
    FileInputStream instream = new FileInputStream(new File(
      "client-p12-keystore.p12")); 
    try { 
     trustStore.load(instream, "password".toCharArray()); 
    } finally { 
     instream.close(); 
    } 

    // Trust own CA and all self-signed certs 
    SSLContext sslcontext = SSLContexts.custom() 
      .loadKeyMaterial(keyStore, "password".toCharArray()) 
      // .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()) //if you have a trust store 
      .build(); 
    // Allow TLSv1 protocol only 
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
      sslcontext, new String[] { "TLSv1" }, null, 
      SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
    CloseableHttpClient httpclient = HttpClients 
      .custom() 
      .setHostnameVerifier(
        SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER) //todo 
      .setSSLSocketFactory(sslsf).build(); 
    try { 

     HttpGet httpget = new HttpGet("https://localhost:8443/secure/index"); 

     System.out.println("executing request" + httpget.getRequestLine()); 

     CloseableHttpResponse response = httpclient.execute(httpget); 
     try { 
      HttpEntity entity = response.getEntity(); 

      System.out.println("----------------------------------------"); 
      System.out.println(response.getStatusLine()); 
      if (entity != null) { 
       System.out.println("Response content length: " 
         + entity.getContentLength()); 
      } 
      EntityUtils.consume(entity); 
     } finally { 
      response.close(); 
     } 
    } finally { 
     httpclient.close(); 
    } 
} 
+0

स्रोत के लिए एक लिंक देखें उपयोगी होगा ... –

+1

@EvilRaat http://hc.apache.org/httpcomponents-client-4.3.x/httpclient/examples/org/apache/http/examples/client/ClientCustomSSL.java http: // hc से .apache.org/httpcomponents-ग्राहक-4.3.x/examples.html – EpicPandaForce