Spring Boot: remove jsessionid from url

你离开我真会死。 提交于 2019-12-01 00:20:31

I created a quick-and-dirty spring-boot app and here's what I came up with.

The ServletInitializer that is generated, you can alter it in this fashion:

package com.division6.bootr;

import java.util.Collections;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SessionCookieConfig;
import javax.servlet.SessionTrackingMode;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // This can be done here or as the last step in the method
        // Doing it in this order will initialize the Spring
        // Framework first, doing it as last step will initialize
        // the Spring Framework after the Servlet configuration is 
        // established
        super.onStartup(servletContext);

        // This will set to use COOKIE only
        servletContext
            .setSessionTrackingModes(
                Collections.singleton(SessionTrackingMode.COOKIE)
        );
        // This will prevent any JS on the page from accessing the
        // cookie - it will only be used/accessed by the HTTP transport
        // mechanism in use
        SessionCookieConfig sessionCookieConfig=
                servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringBootrApplication.class);
    }

}

AUTHOR NOTE

I am not 100% sure when this was introduced but by introducing the following parameters, the same can be accomplished without having to write code:

  • server.servlet.session.cookie.http-only=true
  • server.servlet.session.tracking-modes=cookie

As this question is in spring boot context, easy solution for me was:

server:
  session:
    tracking-modes: cookie

Added in appication.yml it modifies embedded tomcat config. From list of ll spring boot properties: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

you can also try this,

        @Bean
            public ServletContextInitializer servletContextInitializer() {
                return new ServletContextInitializer() {

                    @Override
                    public void onStartup(ServletContext servletContext) throws ServletException {
                       servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
                       SessionCookieConfig sessionCookieConfig=servletContext.getSessionCookieConfig();
                       sessionCookieConfig.setHttpOnly(true);
                    }
                };

        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!