解决spring boot项目中链接多出jsessionid的问题

在HTML中用<input id="urlcontext" type="hidden" th:value="@{/}" /> 时会发现得到的链接会是  " /项目名/;jsessionid=xxxxxxxxxxxxxxxxxxxx"  这样的形式,这样再去拼接链接访问就会报错。

 

  在启动类上继承   SpringBootServletInitializer  类,然后重写  onStartup  方法:

public void onStartup(ServletContext servletContext) throws ServletException{
        super.onStartup(servletContext);
        servletContext.setSessionTrackingModes(Collections.singleton(SessionTrackingMode.COOKIE));
        SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);
}

  这样就可以解决URL中出现的jsessionid的问题。

  

  然后有个问题是

  我在spring boot 2.0.0 RELEASE版本中使用内嵌Tomcat启动时,在application.yml中增加如下:

server: 
  session: 
    tracking-modes: cookie

 发现不生效,org.springframework.boot.autoconfigure.web.ServerProperties 这个类也没有trackingModes这个属性。如果有人知道原因,望能在评论中告知,不胜感激。

posted @ 2018-03-19 12:09  way2a  阅读(9500)  评论(3编辑  收藏  举报