java Spring Boot:从 url 中删除 jsessionid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31791587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot: remove jsessionid from url
提问by Kian
How can I remove the jsessionid from my urls?
如何从我的网址中删除 jsessionid?
I'm using Spring Boot MVC (without Spring Security; tomcat embedded).
我正在使用 Spring Boot MVC(没有 Spring Security;嵌入了 tomcat)。
I've read that It could be done by setting the disableUrlRewriting to "true". But this looks like a Spring Security solution, which I don't use (it's a simple project without login; just pages; a session-controller exists and has to be a session-controller).
我读过可以通过将 disableUrlRewriting 设置为“true”来完成。但这看起来像一个 Spring Security 解决方案,我不使用它(这是一个没有登录的简单项目;只是页面;会话控制器存在并且必须是会话控制器)。
I'm asking this because GoogleBot is creating urls containing the id.
我问这个是因为 GoogleBot 正在创建包含 id 的网址。
EDIT: I solved it with the solution described at: https://randomcoder.org/articles/jsessionid-considered-harmful
编辑:我用以下描述的解决方案解决了这个问题:https: //randomcoder.org/articles/jsessionid-thinked-harmful
回答by Dave G
I created a quick-and-dirty spring-boot app and here's what I came up with.
我创建了一个快速而肮脏的 spring-boot 应用程序,这就是我想出的。
The ServletInitializer that is generated, you can alter it in this fashion:
生成的 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);
}
}
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:
我不是 100% 确定这是何时引入的,但是通过引入以下参数,无需编写代码即可完成相同的操作:
- server.servlet.session.cookie.http-only=true
- server.servlet.session.tracking-modes=cookie
- server.servlet.session.cookie.http-only=true
- server.servlet.session.tracking-modes=cookie
回答by most_wanted
As this question is in spring boot context, easy solution for me was:
由于这个问题是在 spring boot 上下文中,对我来说简单的解决方案是:
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
在 appication.yml 中添加它修改嵌入式 tomcat 配置。从 ll spring boot 属性列表:https: //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
回答by Saurabh
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);
}
};
}
回答by Alex R
More portable option which also works for non-SpringBoot, add the following to the webapp's web.xml
:
更便携的选项也适用于非 SpringBoot,将以下内容添加到 webapp 的web.xml
:
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>