繁体   English   中英

Spring Boot:从 url 中删除 jsessionid

[英]Spring Boot: remove jsessionid from url

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

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

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

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

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

由于这个问题是在 spring boot 上下文中,对我来说简单的解决方案是:

server:
  session:
    tracking-modes: cookie

春季 2 之后

server:
  servlet
    session:
      tracking-modes: cookie

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

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

}

作者注

我不是 100% 确定这是何时引入的,但是通过引入以下参数,无需编写代码即可完成相同的操作:

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

你也可以试试这个

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

        }

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM