3

개발 목적으로 자체 서명 된 인증서를 사용하여 보안 http 연결을 테스트하려고합니다. 하지만 피어 인증되지 않은 예외를 해결할 수없는, 물론 나는이 예외에 대한 유사한 게시물 살펴본 다음 하나는 현재 내가 사용하고 구현 :httpClient의 SSLPeerUnverifiedException

public class SelfCertificatesSocketFactory extends SSLSocketFactory { 

SSLContext sslContext = SSLContext.getInstance("TLS"); 

public SelfCertificatesSocketFactory(KeyStore trustStore) throws NoSuchAlgorithmException,UnrecoverableKeyException,KeyStoreException,KeyManagementException { 
    super(trustStore); 

     TrustManager tm = new X509TrustManager() { 
      public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      } 

      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { 
      } 

      public X509Certificate[] getAcceptedIssuers() { 
       return null; 
      } 
     }; 




} 

@Override 
public Socket createSocket() throws IOException { 
    return sslContext.getSocketFactory().createSocket(); 
} 



@Override 
public Socket createSocket(Socket socket, String host, int port, 
     boolean autoClose) throws IOException, UnknownHostException { 
    return sslContext.getSocketFactory().createSocket(socket,host,port,autoClose); 
} 



} 

그리고 사용 :

private DefaultHttpClient createHttpsClient(){ 
    try { 
     KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); 
     trustStore.load(null, null); 

     SSLSocketFactory sf = new SelfCertificatesSocketFactory(trustStore); 
     //sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("https", 443, sf)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); 
     return new DefaultHttpClient(ccm); 
    } catch (Exception e) { 
     return new DefaultHttpClient(); 
    } 

} 

그러나 작동하지 않습니다 ... 아직 예외가 발생합니다. 내가 뭘 잘못하고 있니? PD : 자바 웹 애플리케이션을 구현하고 있습니다. Android 클라이언트가 아닙니다. 고마워요.

+0

당신은 JVM의 trustore에 인증서를 가져온? – kosa

+0

아니, 그 방법에 대해 잘 모르겠습니다 ... 내가 가진 전부는 keytool에 의해 생성 된 .keystore 파일입니다. – Pablo

+0

완전한 질문에 감사드립니다. 전에이 코드를 본 적이 없었습니다. '''KeyStore trustStore = KeyStore.getInstance (KeyStore.getDefaultType()); trustStore.load (null, null); SSLSocketFactory sf = 새로운 SelfCertificatesSocketFactory (트러스트 스토어);''' – pulkitsinghal

답변

2

코드로 작성된 트러스트 매니저 인스턴스가 어디에도 사용되지 않는 것처럼 보이고 키 스토어 인스턴스에 트러스트 자료가 들어 있지 않은 것 같습니다.

모든 것을 수행하는 대신 SSLSocketFactory의 기능을 활용해야합니다. 다음 코드를 사용하여 해결을 만든

TrustStrategy easyStrategy = new TrustStrategy() { 
    public boolean isTrusted(X509Certificate[] chain, String authType) 
      throws CertificateException { 
     // eh, why not? 
     return true; 
    } 
}; 
SSLSocketFactory sf = new SSLSocketFactory(easyStrategy); 
2

감사합니다, 내 특정 경우

HttpClient client = null; 
    TrustStrategy easyStrategy = new TrustStrategy() { 

     @Override 
     public boolean isTrusted(X509Certificate[] certificate, String authType) 
       throws CertificateException { 
      return true; 
     } 
    }; 
    try { 

     SSLSocketFactory sf = new SSLSocketFactory(easyStrategy,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 
     SchemeRegistry registry = new SchemeRegistry(); 
     registry.register(new Scheme("https", 8443, sf)); 

     ClientConnectionManager ccm = new ThreadSafeClientConnManager(registry); 
     client = new DefaultHttpClient(ccm); 

    } catch (KeyManagementException e1) { 
     e1.printStackTrace(); 
    } catch (UnrecoverableKeyException e1) { 
     e1.printStackTrace(); 
    } catch (NoSuchAlgorithmException e1) { 
     e1.printStackTrace(); 
    } catch (KeyStoreException e1) { 
     e1.printStackTrace(); 
    } 
1

는 내 장치의 시스템 시간은 과거에 설정되었다. 겉으로는 명백한 지적 this page

감사합니다 ... :)