Spring 引导:从 url 中删除 jsessionid
Spring Boot: remove jsessionid from url
如何从我的网址中删除 jsessionid?
我正在使用 Spring Boot MVC(没有 Spring 安全;tomcat 嵌入)。
我读到可以通过将 disableUrlRewriting 设置为 "true" 来完成。
但这看起来像一个 Spring 安全解决方案,我不使用它(这是一个没有登录的简单项目;只是页面;会话控制器存在并且必须是会话控制器)。
我问这个是因为 GoogleBot 正在创建包含 ID 的网址。
编辑:
我用以下描述的解决方案解决了它:https://randomcoder.org/articles/jsessionid-considered-harmful
我创建了一个快捷的 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-仅=true
- server.servlet.session.tracking-模式=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);
}
};
}
因为这个问题是在 spring 引导上下文中,所以对我来说简单的解决方案是:
server:
session:
tracking-modes: cookie
spring 2 之后
server:
servlet
session:
tracking-modes: cookie
在 appication.yml 中添加它修改嵌入式 tomcat 配置。从 ll spring 启动属性列表:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
更便携的选项也适用于非 Spring Boot,将以下内容添加到 webapps web.xml
:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
如何从我的网址中删除 jsessionid?
我正在使用 Spring Boot MVC(没有 Spring 安全;tomcat 嵌入)。
我读到可以通过将 disableUrlRewriting 设置为 "true" 来完成。 但这看起来像一个 Spring 安全解决方案,我不使用它(这是一个没有登录的简单项目;只是页面;会话控制器存在并且必须是会话控制器)。
我问这个是因为 GoogleBot 正在创建包含 ID 的网址。
编辑: 我用以下描述的解决方案解决了它:https://randomcoder.org/articles/jsessionid-considered-harmful
我创建了一个快捷的 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-仅=true
- server.servlet.session.tracking-模式=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);
}
};
}
因为这个问题是在 spring 引导上下文中,所以对我来说简单的解决方案是:
server:
session:
tracking-modes: cookie
spring 2 之后
server:
servlet
session:
tracking-modes: cookie
在 appication.yml 中添加它修改嵌入式 tomcat 配置。从 ll spring 启动属性列表: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
更便携的选项也适用于非 Spring Boot,将以下内容添加到 webapps web.xml
:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>