Spring Boot: 移除URL中的JSESSIONID

8

如何从我的网址中删除jsessionid?

我正在使用Spring Boot MVC(没有Spring Security;嵌入式Tomcat)。

我已经了解到可以通过将disableUrlRewriting设置为“true”来实现。但是这似乎是Spring Security的解决方案,而我不使用它(这是一个简单的项目,没有登录;只有页面;存在session-controller并且必须是session-controller)。

我之所以问这个问题是因为GoogleBot正在创建包含该ID的网址。

编辑: 我用在https://randomcoder.org/articles/jsessionid-considered-harmful上描述的解决方案解决了这个问题。

4个回答

12

3
Spring Boot 2.0 应该是 server.servlet.session.tracking-modes=cookie,请查看上面 @DaveG 的回答。 - Keijack

8

我创建了一个快速且简单的Spring Boot应用,以下是我的成果。

生成的ServletInitializer,你可以按照以下方式进行修改:

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);
    }

}

作者说明

我不确定这是什么时候引入的,但通过引入以下参数,就可以在无需编写代码的情况下完成相同的操作:

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

4

你也可以尝试这个方法:

        @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);
                    }
                };

        }

1

更为便携的选项,也适用于非SpringBoot项目,将以下内容添加到webapp的web.xml中:

<session-config>
    <tracking-mode>COOKIE</tracking-mode>
</session-config>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接