2016-07-22 140 views
0

我在HttpsURLConnection的是的ConfigEd使用一个定制的TrustManager如下项目:如何让Jersey客户端使用defaultSSLSocketFactory在HttpsURLConnection的设置?

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

有一个在这个项目REST API客户端,它使用Jersey客户端发送HTTP/HTTPS请求:

Client client = ClientBuilder.newClient(); 

然而,在这Jerset客户端发起的HTTPS连接不使用defaultSSLSocketFactory我设置HttpsURLConnection的,它无法连接到不可信的HTTPS URL。

我需要明确设置的SSLContext此客户端上,以使其与我的TrustManager工作。

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

有什么办法可以解决这个问题吗?

谢谢。

+0

是否http://stackoverflow.com/questions/ 2145431/https-using-jersey-client回答这个问题? –

+0

否。该文章教导如何通过Jersey将请求发送到HTTPS URL。我知道如何做到这一点(如最后一段所示)。我不知道的是如何让Jersy客户端使用我在HttpsURLConnection中设置的SslContext。我想这样做的原因是我没有权限编辑REST API客户端代码以明确地在Jersey客户端上设置SslContext。 – Lee

回答

1

的解决方案,我最终发现是对的SSLSocketFactory提供商属性设置为自定义SSLSocketFactory的。希望这可以帮助其他有类似问题的人。

称这种现象为程序的开头:

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

这里是MySSLSocketFactory的样子(还设置连接超时):

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); 
    }