2016-07-22 9 views
0

Ich habe ein Projekt, in dem HttpsURLConnection configed ist eine angepasste Trustmanager wie folgt zu verwenden:Wie kann Jersey-Client die in HttpsURLConnection festgelegte defaultSSLSocketFactory verwenden?

SSLContext sslContext = SSLContext.getInstance("SSL"); 
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null); 
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); 

ein REST Es gibt in diesem Projekt API-Client ist, verwendet es Jersey-Client HTTP/HTTPS-Anfrage senden:

Die von diesem Jerset-Client initiierte HTTPS-Verbindung verwendet jedoch nicht die in HttpsURLConnection festgelegte defaultSSLSocketFactory, und es wird keine Verbindung zur nicht vertrauenswürdigen HTTPS-URL hergestellt.

Ich muss den SslContext explizit auf diesem Client festlegen, damit es mit meinem TrustManager funktioniert.

SSLContext sslContext = SSLContext.getInstance("SSL"); 
sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null); 
Client client = ClientBuilder.newBuilder().sslContext(sslContext).build(); 

Gibt es eine Möglichkeit, dieses Problem zu lösen?

Danke.

+0

Does http://stackoverflow.com/questions/ 2145431/https-using-jersey-client antwortest du? –

+0

Nein. Dieser Beitrag lehrt, wie Sie eine Anfrage per HTTPS-URL über Jersey senden. Ich weiß, wie man das macht (wie im letzten Ausschnitt gezeigt). Was ich nicht weiß ist, wie der Jersy-Client den SslContext verwenden kann, den ich in HttpsURLConnection eingestellt habe. Der Grund, warum ich dies tun möchte, ist, dass ich keine Berechtigung habe, den REST-API-Client-Code zu bearbeiten, um SslContext explizit auf dem Jersey-Client zu setzen. – Lee

Antwort

1

Die Lösung, die ich schließlich fand, besteht darin, die SSLSocketFactory-Provider-Eigenschaft auf eine angepasste SSLSocketFactory festzulegen. Hoffe, das kann anderen helfen, die ähnliche Probleme haben.

Rufen Sie diese in Anfang des Programms:

Security.setProperty("ssl.SocketFactory.provider", MySSLSocketFactory.class.getCanonicalName()); 

Hier ist, wie MySSLSocketFactory aussieht (es auch Connection Timeout setzt):

public class MySSLSocketFactory extends SSLSocketFactory { 

    private SSLContext sslContext = SSLContext.getInstance(Const.Ssl.PROTOCOL_SSL); 


    public MySSLSocketFactory() throws NoSuchAlgorithmException, KeyManagementException { 
     this.sslContext.init(
       null, 
       new TrustManager[] { new MyTrustManager(false) }, 
       new SecureRandom()); 
    } 


    @Override 
    public Socket createSocket(Socket socket, String host, int port, boolean autoClose) 
      throws IOException { 
     socket.connect(new InetSocketAddress(host, port), Const.Ssl.CONNECT_TIMEOUT); 
     socket.setSoTimeout(Const.Ssl.DATA_TIMEOUT); 
     return this.sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); 
    } 


    @Override 
    public String[] getDefaultCipherSuites() { 
     return this.sslContext.getSocketFactory().getDefaultCipherSuites(); 
    } 


    @Override 
    public String[] getSupportedCipherSuites() { 
     return this.sslContext.getSocketFactory().getSupportedCipherSuites(); 
    } 


    @Override 
    public Socket createSocket(String host, int port) 
      throws IOException, UnknownHostException { 
     return this.createSocket(new Socket(), host, port, true); 
    } 


    @Override 
    public Socket createSocket(InetAddress address, int port) 
      throws IOException { 
     return this.createSocket(new Socket(), address.getHostAddress(), port, true); 
    } 


    @Override 
    public Socket createSocket(String host, int port, InetAddress localHost, int localPort) 
      throws IOException, UnknownHostException { 
     return this.createSocket(new Socket(), host, port, true); 
    } 


    @Override 
    public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) 
      throws IOException { 
     return this.createSocket(new Socket(), address.getHostAddress(), port, true); 
    } 
Verwandte Themen