zoukankan      html  css  js  c++  java
  • Spring Boot: remove jsessionid from url

    参考代码 :Spring Boot: remove jsessionid from url

    我的SpringBoot用2.0.*,答案中的第一二个方案亲测无效。

    应该在继承了Configuration里面加入第三种方案所示的代码

    @Configuration
    //WebMvcConfigurerAdapter在2.0.*中已作废,有WebMvcConfigurer,WebMvcConfigurationSupport两种方案。
    //public class WebSecurityConfig extends WebMvcConfigurerAdapter{
    public class WebSecurityConfig implements WebMvcConfigurer {
    //public class WebSecurityConfig extends WebMvcConfigurationSupport {
                @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);
                        }
                    };
    
            }
    
    }

    或者

    @Configuration
    //WebMvcConfigurerAdapter在2.0.*中已作废,有WebMvcConfigurer,WebMvcConfigurationSupport两种方案。
    //public class WebSecurityConfig extends WebMvcConfigurerAdapter{
    public class WebSecurityConfig implements WebMvcConfigurer {
    //public class WebSecurityConfig extends WebMvcConfigurationSupport {
    
        @Bean
        public ServletContextInitializer servletContextInitializer() {
            return servletContext -> {
                servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
                SessionCookieConfig sessionCookieConfig=servletContext.getSessionCookieConfig();
                sessionCookieConfig.setHttpOnly(true);
            };
    
        }
    }

    可以看到该段代码实现了以下接口

    package org.springframework.boot.web.servlet;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    
    @FunctionalInterface
    public interface ServletContextInitializer {
        void onStartup(ServletContext servletContext) throws ServletException;
    }
  • 相关阅读:
    对于线程同步的浅薄理解
    线程安全之ConcurrentQueue<T>队列
    关于mybatis拦截器,对结果集进行拦截
    oracle 分析函数
    C# ikvm 运行htmlunit Provider com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl not found
    IronJs 无相关源?
    js div 内容显示分页
    JavascriptTAB切换 AND JqueryTAB切换
    php中mysql数据库操作类 -李盛鹏 -博客园
    sublime text 之snippet功能的使用 -李盛鹏 -博客园
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/8919926.html
Copyright © 2011-2022 走看看