Spring Boot:从url中删除jsessionid

Kia*_*ian 3 java spring spring-mvc tomcat8

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

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

我已经读过可以通过将disableUrlRewriting设置为"true"来完成.但这看起来像是一个Spring Security解决方案,我没有使用它(这是一个没有登录的简单项目;只是页面;会话控制器存在并且必须是会话控制器).

我问这个是因为GoogleBot正在创建包含id的网址.

编辑:我用以下描述的解决方案解决了它:https://randomcoder.org/articles/jsessionid-considered-harmful

Dav*_*e G 6

我创建了一个快速且肮脏的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);
    }

}
Run Code Online (Sandbox Code Playgroud)

作者注

我不确定该何时引入,但通过引入以下参数,无需编写代码即可实现相同的目的:

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


小智 5

由于这个问题是在春季启动环境中,对我来说简单的解决方案是:

server:
  session:
    tracking-modes: cookie
Run Code Online (Sandbox Code Playgroud)

在appication.yml中添加它修改了嵌入式tomcat配置.从ll spring引导属性列表:https: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

  • spring-boot 2.0 应该是`server.servlet.session.tracking-modes=cookie`,检查上面@DaveG 的回答。 (2认同)