Changing the session cookie name

I am using Spark Java with embedded Jetty to run my application. An application is a small web application with multiple integrations with specific external services depending on a specific configuration.

The requirement is that the client can perform multiple integrations on the same server at different ports, for example, for applications running on different ports 8080 and 8084 with integration enabled. The problem is that the session cookie collides when the user tries to access both applications in the same browser and creates problems. Is there a way to change the session variable from JSESSIONID to something else? Or is there a way I can tell Spark to enable the port on the cookie domain too?

I tried setting the cookie domain with getServletContext().getSessionCookieConfig().setDomain()

but it getServletContext()

always returns NULL.

Any help is greatly appreciated. Thank,

+3


source to share


1 answer


This pull, requested since March 2017, allowed for fine tuning of the built jetty as follows:

public class Main {
    public static void main(String ...args) throws Exception {
        EmbeddedServers.add(EmbeddedServers.Identifiers.JETTY, (Routes routeMatcher, StaticFilesConfiguration staticFilesConfiguration, boolean hasMultipleHandler) -> {
            MatcherFilter matcherFilter = new MatcherFilter(routeMatcher, staticFilesConfiguration, false, hasMultipleHandler);
            matcherFilter.init(null);

            JettyHandler handler = new JettyHandler(matcherFilter);
            handler.getSessionCookieConfig().setName("XSESSION");

            return new EmbeddedJettyServer((int maxThreads, int minThreads, int threadTimeoutMillis) -> {
                return new Server();
            }, handler);
        });

        get("/hello", (req, res) -> {
            req.session(true);
            return "Hello World";
        });
    }
}

      

now you can check the results with curl like this:



curl -v localhost:4567/hello

      

and the resulting result will give you the following title Set-Cookie

:

Set-Cookie: XSESSION=node01j56de4fpp69kl2ye6br6cvno0.node0;Path=/

      

+2


source







All Articles