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