2013-09-29 2 views
0

아래 코드를 작성했습니다. 인증서를 성공적으로 가져 왔습니다. 아무도 예외를 얻는 이유가 무엇인지 말해 줄 수 있습니까? 하나의 IP https://x.x.x.x을 연결하려고하는데 예외가 발생했습니다. 나는 두 가지를 시도했다javax.net.ssl.SSLHandshakeException : java.security.cert.CertificateException

1) 인증서를 전달함으로써 2) 인증서를 다운로드하고 keytool을 사용하여 java 트러스트 스토어에 추가한다. 나를 위해 아무것도 잘 작동하지 않습니다.

package com.dell;  
import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.security.GeneralSecurityException; 
import javax.net.ssl.HttpsURLConnection; 
import javax.net.ssl.SSLContext; 
import javax.net.ssl.TrustManager; 
import javax.net.ssl.X509TrustManager; 

public class HttpURLConnectionExample { 
    private final String USER_AGENT = "Mozilla/5.0"; 

    public static void main(String[] args) throws Exception { 
     HttpURLConnectionExample http = new HttpURLConnectionExample(); 
     System.out.println("Testing 1 - Send Http GET request"); 
     http.sendGet(); 
     System.out.println("\nTesting 2 - Send Http POST request"); 
     http.sendPost(); 

    } 

    // HTTP GET request 
    private void sendGet() throws Exception { 
     URL obj = null; 
     TrustManager[] trustAllCerts = new TrustManager[] { 
       new X509TrustManager() {  
        public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
         return null; 
        } 
        public void checkClientTrusted( 
         java.security.cert.X509Certificate[] certs, String authType) { 
         } 
        public void checkServerTrusted( 
         java.security.cert.X509Certificate[] certs, String authType) { 
        } 
       } 
      }; 

     // Install the all-trusting trust manager 
     try { 
      SSLContext sc = SSLContext.getInstance("SSL"); 
      sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
      HttpsURLConnection 
        .setDefaultSSLSocketFactory(sc.getSocketFactory()); 
     } catch (GeneralSecurityException e) { 
     } 
     try { 
      obj = new URL("https://10.94.218.114"); 
     } catch (MalformedURLException e) { 
     } 

     HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 
     con.setRequestMethod("GET");   
     con.setRequestProperty("User-Agent", USER_AGENT); 
     int responseCode = con.getResponseCode(); 
     System.out.println("Response Code : " + responseCode); 
     BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 
     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 

     //print result 
     System.out.println(response.toString()); 

    } 

    // HTTP POST request 
    private void sendPost() throws Exception { 

     String url = "https://selfsolve.apple.com/wcResults.do"; 
     URL obj = new URL(url); 
     HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

     //add request header 
     con.setRequestMethod("POST"); 
     con.setRequestProperty("User-Agent", USER_AGENT); 
     con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 

     String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; 

     // Send post request 
     con.setDoOutput(true); 
     DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
     wr.writeBytes(urlParameters); 
     wr.flush(); 
     wr.close(); 

     int responseCode = con.getResponseCode(); 
     System.out.println("\nSending 'POST' request to URL : " + url); 
     System.out.println("Post parameters : " + urlParameters); 
     System.out.println("Response Code : " + responseCode); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(con.getInputStream())); 
     String inputLine; 
     StringBuffer response = new StringBuffer(); 

     while ((inputLine = in.readLine()) != null) { 
      response.append(inputLine); 
     } 
     in.close(); 
     System.out.println(response.toString()); 

    } 

} 
+0

예외를 모두 표시 할 수 있습니까? 인증서에 무엇이 잘못되었는지 표시해야합니다. – Thilo

+0

스레드 "main"의 예외 javax.net.ssl.SSLHandshakeException : java.security.cert.CertificateException : 주제 대체 이름이 없습니다. \t at sun.security.ssl.Alerts.getSSLException (알 수없는 소스) \t at sun.security .ssl.SSLSocketImpl.fatal (알 소스)에 sun.security.ssl.Handshaker.fatalSE \t (알 소스)에 sun.security.ssl.Handshaker.fatalSE \t (알 소스)에 \t sun.security.ssl .ClientHandshaker.serverCertificate (알 수없는 출처) \t (sun.security.ssl.ClientHandshaker.processMessage) 알 수없는 원본) – amiton2006

+0

발생 원인 : java.security.cert.CertificateException : 주제 대체 이름이 없습니다. \t at sun.security.util.HostnameChecker.matchIP (알 수없는 소스) – amiton2006

답변

5

정확히 this question과 같은 문제입니다. 기본 Java 호스트 이름 확인 프로그램은 IP 주소를 처리 할 때 RFC 2818을 엄격하게 구현합니다.

경우에 따라 URI가 호스트 이름이 아닌 IP 주소로 지정되는 경우도 있습니다. 이 경우 iPAddress subjectAltName이 인증서에 있어야하며 URI의 IP와 정확하게 일치해야합니다.

특히 SAN에서 IP 주소를 찾을 수없는 경우 주체 DN의 CN으로 되돌아 가지 않습니다.

기본적으로 해당 서비스에 대한 인증서가 HTTPS 사양과 호환되지 않습니다 (그러나 일부 브라우저는 어쨌든이를 수락합니다).

해당 서버를 제어하는 ​​사람과 연락하고이 IP 주소에 대해 SAN 항목이있는 인증서를 입력하도록 요청해야합니다 (항목 유형이 "DNS"가 아닌 "IP 주소"여야 함을 유의하십시오)).

관련 문제