解決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這個屬性。如果有人知道原因,望能在評論中告知,不勝感激。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM