Spring boot中去掉URL后面的jsessionid

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:17   1066   0

url中有Jsessionid生成的原因:


jsessionid是标明session的id,它存在于cookie中,一般情况不会出现在url中,服务器会从客户端的cookie中取出来,但是如果客户端禁用了cookie的话,就要重写url了,显式的将jsessionid重写到Url中,方便服务器来通过这个找到session的id。

如果客户端请求的cookie中不包含JSESSIONID,服务端调用request.getSession()时就会生成并传递给客户端,此次响应头会包含设置cookie的信息

如果客户端请求的cookie中包含JSESSIONID,服务端调用request.getSession()时就会根据JSESSIONID进行查找对象,如果能查到就返回,否则就跟没传递JSESSIONID一样;


解决方式一:

springBoot2.0之前版本

在 .yml配置文件中做如下配置

解决方式二:

在启动类中继承SpringBootServletInitializer,然后重写这个方法 (此方法在springBoot2.0之前版本没有起作用,暂时做记录)

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);
    }


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP