Search code examples
apache-httpcomponents

How to disable Session cookie in Apache HttpAsyncClientBuilder


I'm talking to a service that fails in getting the user authentication cookie if there is a JSESSIONID cookie in the request, and I can't modify this service. It also returns this session cookie on each response, so my first request work (no other cookie than the user's one), but next requests will always fail.

My restTemplate configuration uses a custom request factory that extends Spring's HttpComponentsAsyncClientHttpRequestFactory with an AsyncClient from Apache's HttpAsyncClientBuilder.

Is there a way to configure that to always ignore the session cookie ?

Thanks in advance!


Solution

  • It would have been nice to find a solution impliying only configuration, but I couldn't so I ended up extending the BasicCookieStore:

    public class DefaultCookieStore extends BasicCookieStore {
    
        private static String SESSION_COOKIE_NAME = "JSESSIONID";
    
        @Override
        public void addCookie(Cookie cookie) {
            if (!SESSION_COOKIE_NAME.equals(cookie.getName())) {
                super.addCookie(cookie);
            }
        }
    }
    

    And adding it to my HttpAsyncClientBuilder and HttpClientBuilder with the method setDefaultCookieStore.

    Probably not the best thing, but it works well.