Commit 936a3b33 by Dave Syer

Set explicit cookie policy in HTTP client in RibbonClientConfiguration

When the Apache HTTP client is being set up for the Ribbon client we can jump in and explicitly set a cookie policy. Obviously, since the client will generally be shared across sessions, the only sensible default is IGNORE_COOKIES. I couldn't find a way round this without using deprecated APIs (but there are other instances of that in our code because of the way Netflix uses it, so maybe that doesn't matter). Fixes gh-301
parent 17998095
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
...@@ -37,6 +39,8 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule; ...@@ -37,6 +39,8 @@ import com.netflix.loadbalancer.ZoneAvoidanceRule;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer; import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import com.netflix.niws.client.http.RestClient; import com.netflix.niws.client.http.RestClient;
import com.netflix.servo.monitor.Monitors; import com.netflix.servo.monitor.Monitors;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.client.apache4.ApacheHttpClient4;
/** /**
* @author Dave Syer * @author Dave Syer
...@@ -86,7 +90,7 @@ public class RibbonClientConfiguration { ...@@ -86,7 +90,7 @@ public class RibbonClientConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RestClient ribbonRestClient(IClientConfig config, ILoadBalancer loadBalancer) { public RestClient ribbonRestClient(IClientConfig config, ILoadBalancer loadBalancer) {
RestClient client = new RestClient(config); RestClient client = new OverrideRestClient(config);
client.setLoadBalancer(loadBalancer); client.setLoadBalancer(loadBalancer);
Monitors.registerObject("Client_" + this.name, client); Monitors.registerObject("Client_" + this.name, client);
return client; return client;
...@@ -119,4 +123,23 @@ public class RibbonClientConfiguration { ...@@ -119,4 +123,23 @@ public class RibbonClientConfiguration {
return new RibbonLoadBalancerContext(loadBalancer, config); return new RibbonLoadBalancerContext(loadBalancer, config);
} }
private static class OverrideRestClient extends RestClient {
private OverrideRestClient(IClientConfig ncc) {
super(ncc);
}
@Override
protected Client apacheHttpClientSpecificInitialization() {
ApacheHttpClient4 apache = (ApacheHttpClient4) super
.apacheHttpClientSpecificInitialization();
apache.getClientHandler()
.getHttpClient()
.getParams()
.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);
return apache;
}
}
} }
...@@ -16,12 +16,16 @@ ...@@ -16,12 +16,16 @@
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.test.EnvironmentTestUtils; import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration; import org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.netflix.client.DefaultLoadBalancerRetryHandler; import com.netflix.client.DefaultLoadBalancerRetryHandler;
import com.netflix.niws.client.http.RestClient;
import com.sun.jersey.client.apache4.ApacheHttpClient4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
...@@ -46,4 +50,12 @@ public class SpringClientFactoryTests { ...@@ -46,4 +50,12 @@ public class SpringClientFactoryTests {
this.factory.destroy(); this.factory.destroy();
} }
@Test
public void testCookiePolicy() {
RestClient client = this.factory.getClient("foo", RestClient.class);
ApacheHttpClient4 jerseyClient = (ApacheHttpClient4) client.getJerseyClient();
assertEquals(CookiePolicy.IGNORE_COOKIES, jerseyClient.getClientHandler()
.getHttpClient().getParams().getParameter(ClientPNames.COOKIE_POLICY));
this.factory.destroy();
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment