Spring boot中出现图片URL后面的jsessionid情况,导致图片加载失败,需重新刷新加载

旧巷老猫 提交于 2019-12-11 03:25:18

jsessionid是用来指明session的id,存在于cookie中,当客户端禁用cookie时,第一次服务器将拿不到客户端的cookie,导致url重写,从而将jesessionid写入至url中,自然而然图片的地址也匹配不对。


解决方案:
springBoot2.0之前版本

 

在application.properties中配置文件

#使用会话cookie追踪会话ID
server.session.tracking-modes=cookie
#设置客户端cookie为可用
server.session.cookie.http-only=true

springboot2.0之后版本

在启动类中继承SpringBootServletInitializer,然后重写这个方法  

public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
 
        // This will set to use COOKIE only
        servletContext.setSessionTrackingModes(
                Collections.singleton(SessionTrackingMode.COOKIE)
        );
        // This will prevent any JS on the page from accessing the
        // cookie - it will only be used/accessed by the HTTP transport
        // mechanism in use
        SessionCookieConfig sessionCookieConfig =
                servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);
    }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!