2014-02-14 27 views
10

我有一個設置託管我的REST服務器的Tomcat服務器重定向從HTTP(端口9080)到HTTPS(端口9443)的調用。澤西jax.rs客戶端2.5遵循重定向

我使用球衣2.5實現,無法管理配置客戶端遵循重定向。

我發現了這個問題(Jersey client doesn't follow redirects),但它提供給運動衫1.X系列和API已更改。

我試着用下面的測試代碼,以使其適用於2.5:

SSLContextProvider ssl = new TrustAllSSLContextImpl(); // just trust all certs 
Response response =  ClientBuilder.newBuilder() 
    .sslContext(ssl.getContext()).newClient() 
    .register(LoggingFilter.class) 
    .target("http://testhost.domain.org:9080/rest.webapp/api/v1/hello/") 
    .property(ClientProperties.FOLLOW_REDIRECTS, Boolean.TRUE) 
    .request().get(); 
Assertions.assertThat(response.getStatus()).isNotEqualTo(302); 

從而未能爲客戶似乎並不遵循重定向。以下是日誌過濾器提供:

Feb 14, 2014 12:23:45 PM org.glassfish.jersey.filter.LoggingFilter log 
INFO: 1 * Sending client request on thread main 
1 > GET http://testhost.domain.org:9080/rest.webapp/api/v1/hello/ 

Feb 14, 2014 12:23:45 PM org.glassfish.jersey.filter.LoggingFilter log 
INFO: 1 * Client response received on thread main 
1 < 302 
1 < Cache-Control: private 
1 < Content-Length: 0 
1 < Date: Fri, 14 Feb 2014 11:38:59 GMT 
1 < Expires: Thu, 01 Jan 1970 01:00:00 CET 
1 < Location: https://testhost.domain.org:9443/rest.webapp/api/v1/hello/ 
1 < Server: Apache-Coyote/1.1 

從球衣DOC我瞭解,所有需要做的是給ClientProperties.FOLLOW_REDIRECTS屬性添加到客戶端,但是這顯然不是成爲case.I還發現消息表明可能需要客戶端過濾器來遵循重定向,但沒有找到任何示例或準則。

所以,如果有人有一些jax.rs和重定向的經驗可以指向我的方向/文檔/示例代碼,我真的很感激。

回答

6

嗯,我使用的過濾器終於想通了這一點,不知道這是最好的解決方案,任何評論都讚賞:

public class FollowRedirectFilter implements ClientResponseFilter 
{ 
    @Override 
    public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException 
    { 
     if (responseContext.getStatusInfo().getFamily() != Response.Status.Family.REDIRECTION) 
     return; 

     Response resp = requestContext.getClient().target(responseContext.getLocation()).request().method(requestContext.getMethod()); 

     responseContext.setEntityStream((InputStream) resp.getEntity()); 
     responseContext.setStatusInfo(resp.getStatusInfo()); 
     responseContext.setStatus(resp.getStatus()); 
    } 
} 
+0

我找到了答案在這裏http://stackoverflow.com/questions/1884230/urlconnection-doesnt-follow-redirect:有從HTTP重定向到https不適合工作安全原因。 –

8

正確的方式來做到這一點是:

webTarget.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.TRUE); 
+5

你確定最後一個參數是「false」嗎?在我看來,這只是禁用重定向:) – devlearn

+4

這應該是'webTarget.property(ClientProperties.FOLLOW_REDIRECTS,Boolean.TRUE)',正如前面的評論者指出的那樣。 –

+0

這隻適用於澤西島; ClientProperties是一個Jersey類。 JAX-RS尚未指定任何重定向處理選項 –

1

出現這種情況因爲如果重定向期間URL方案發生更改,Http UrlConnection不會遵循重定向。所以可能的解決方案是use alternative client transport connector

這可能看起來像

SSLContextProvider ssl = new TrustAllSSLContextImpl(); // just trust all certs 

JerseyClientBuilder clientBuilder = new JerseyClientBuilder() 
.sslContext(ssl.getContext()) 
.register(LoggingFilter.class); 
clientBuilder.getConfiguration().connectorProvider(new org.glassfish.jersey.apache.connector.ApacheConnectorProvider()); 

JerseyClient client = clientBuilder.build(); 

Response response = client  
.target("http://testhost.domain.org:9080/rest.webapp/api/v1/hello/") 
.property(ClientProperties.FOLLOW_REDIRECTS, Boolean.TRUE) 
.request().get(); 
Assertions.assertThat(response.getStatus()).isNotEqualTo(302);