perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Change session cookie name (JSESSIONID) #1019

Closed SoltauFintel closed 6 years ago

SoltauFintel commented 6 years ago

I run a Spark application twice on a server. The session data gets mixed up. This is probably due to the same JSESSIONID. How do you solve this problem? I can distinguish the applications at runtime. One solution would be to rename JSESSIONID. Can I access the SessionHandler with Spark?

SoltauFintel commented 6 years ago

Solution for Spark 2.7.2:

      sessionIdCookieName = ...
        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(sessionIdCookieName);
            JettyServerFactory serverFactory = new JettyServerFactory() {
                @Override
                public Server create(int maxThreads, int minThreads, int threadTimeoutMillis) {
                    Server server;
                    if (maxThreads > 0) {
                        int max = maxThreads;
                        int min = (minThreads > 0) ? minThreads : 8;
                        int idleTimeout = (threadTimeoutMillis > 0) ? threadTimeoutMillis : 60000;
                        server = new Server(new QueuedThreadPool(max, min, idleTimeout));
                    } else {
                        server = new Server();
                    }
                    return server;
                }

                @Override
                public Server create(ThreadPool threadPool) {
                    return threadPool != null ? new Server(threadPool) : new Server();
                }
            };
            return new EmbeddedJettyServer(serverFactory, handler);
        });

see also https://github.com/perwendel/spark/pull/813/files

In my opinion spark.embeddedserver.jetty.JettyServer should be public.