spring-cloud / spring-cloud/spring-cloud-gateway
Disable Apache client5 cookie manager by default
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 4.9k
- Forks
- 3.5k
- Avg merge
- 20h 57m
- Merged PRs (30d)
- 8
Description
Describe the bug
We have a tomcat between the gateway MVC (spring cloud 2023.0.0), some problem occured with the JDK httpclient (in java 17 they are problem with node), so we use the implementation of apache httpclient, our tomcat use cookie to pass the JSESSION ID (who is use for the session).
The problem is that the httpclient have the cookie manager activated and the httpclient are reused for all request.
I see two ways to correct this issue :
- Disable the cookie manager of the httpclient
- Recreate a new httpclient for all request
How to reproduce
if we test with postman and the http interceptor with gateway mvc
first call, http-interceptio log :
/test
Generated cookie : 2143473103
on postman we removed all cookie, and the second call :
/test
Request cookie : [a=2143473103]
Generated cookie : 1554856040
The previous cookie was found in the interceptor, because when the first call occured, the cookie in the response was add to the cookie manager of the httpclient, and is reused for the next request
Sample
HTTP interceptor for test :
import com.sun.net.httpserver.HttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Main {
public static void main(String[] args) throws IOException {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 9008), 0);
server.createContext("/", exchange -> {
System.out.println(exchange.getRequestURI());
exchange.getRequestHeaders().entrySet().stream().filter(e -> e.getKey().toLowerCase().equals("cookie")).forEach(e -> System.out.println("Request cookie : " + e.getValue()));
Random random = new Random();
int cookie = random.nextInt();
System.out.println("Generated cookie : " + cookie);
exchange.getResponseHeaders().add("Set-Cookie", "a="+cookie);
exchange.getResponseHeaders().add("a", "b");
exchange.sendResponseHeaders(200, "OK".length());
exchange.getResponseBody().write("OK".getBytes(StandardCharsets.UTF_8));
exchange.getResponseBody().close();
});
server.setExecutor(threadPoolExecutor);
server.start();
System.out.println(" Server started on port 9008");
}
}
gateway MVC : (spring initializr )
Main class :
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Configuration
public static class GatewayConfiguration {
@Bean
public RouterFunction<ServerResponse> routerFunction() {
return route()
.POST("/test", http("http://localhost:9008"))
.build();
}
}
}
In maven we add :
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.3.1</version>
</dependency>
And in the application.properties we add : spring.cloud.gateway.mvc.httpclient.type=AUTODETECT
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the GatewayConfiguration RouterFunction and the spring.cloud.gateway.mvc.httpclient.type=AUTODETECT setting, then reproduce the behavior using the provided HttpServer interceptor and application.properties setup. Trace how the Apache client5 implementation handles cookies across requests; done means cookies are not retained by default while the gateway still performs the configured request.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring, spring-boot
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100