Java Spring Boot 为嵌入式服务器配置自定义 jsessionid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25918556/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:30:20  来源:igfitidea点击:

Spring boot configure custom jsessionid for embedded server

javaspring-bootjsessionid

提问by tkruse

I want to configure my servlet context, such as setting a custom jsessionId key (see Changing cookie JSESSIONID name)

我想配置我的 servlet 上下文,例如设置自定义 jsessionId 键(请参阅更改 cookie JSESSIONID 名称

I believe I can use the SpringBootServletInitializerwhen running a WAR file, manipulating the servletContextin onStartup(). However, when I run on an embedded application server, using new SpringApplicationBuilder().run(), I don't know the best place to manipulate the servlet context.

我相信我可以使用SpringBootServletInitializer运行WAR文件时,操纵servletContextonStartup()。但是,当我在嵌入式应用程序服务器上运行时,使用 new SpringApplicationBuilder().run(),我不知道操作 servlet 上下文的最佳位置。

采纳答案by JamieB

As of Spring Boot 1.3you can simply set a configuration property;

Spring Boot 1.3 开始,您可以简单地设置一个配置属性;

Spring Boot 1.3, 1.4, 1.5

弹簧靴 1.3、1.4、1.5

server.session.cookie.name = MYSESSIONID

Spring Boot 2.x

春季启动 2.x

server.servlet.session.cookie.name = MYSESSIONID

A lot simpler than writing a configuration class.

比编写配置类简单得多。

See https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.htmlfor more session related properties.

有关更多会话相关属性,请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

回答by Andy Wilkinson

Declare a ServletContextInitializerbean in your application's configuration:

ServletContextInitializer在应用程序的配置中声明一个bean:

@Bean
public ServletContextInitializer servletContextInitializer() {
    return new ServletContextInitializer() {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.getSessionCookieConfig().setName("yourCookieName");
        }
    };

}

Alternatively, your application class itself can implement ServletContextInitializer:

或者,您的应用程序类本身可以实现ServletContextInitializer

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application implements ServletContextInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        servletContext.getSessionCookieConfig().setName("yourCookieName");
    }

}

回答by jozdoo

with spring session , if you want to change cookie name ,you can do this

使用 spring 会话,如果您想更改 cookie 名称,您可以这样做

@Bean
public DefaultCookieSerializer defaultCookieSerializer(){
    DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
    defaultCookieSerializer.setCookieName("mySessionId");
    return defaultCookieSerializer;
}

i find this in spring session source

我在 spring 会话源中找到了这个

spring-session-1.2.1.RELEASE-sources.jar!/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java

spring-session-1.2.1.RELEASE-sources.jar!/org/springframework/session/config/annotation/web/http/SpringHttpSessionConfiguration.java

    @Autowired(required = false)
public void setCookieSerializer(CookieSerializer cookieSerializer) {
    this.defaultHttpSessionStrategy.setCookieSerializer(cookieSerializer);
}

回答by bhagyashri S. I.

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER).and()
    .csrf().disable();  
}

You can try this as it removes jsession id from URL

你可以试试这个,因为它从 URL 中删除了 jsession id