Tomcat 中的Session与Cookie

1.问题引入

今天在梳理系统的时,查看系统中的session共享的实现方式,目前主流的session共享采用spring-session 与 redis的解决方案,无奈系统架构还在使用struts2 等老旧的框架,发现原来的session共享的思路与spring-session很是相似,都是通过一个filter过滤器并且自己封装一个request实现HttpServletRequestWrapper ,并重写里面的getSession方法。至于具体实现不多说,由于http 这种无状态的协议,服务端保持会话需要同客户端配合。原理就是

获取session时 会有一个session的标志ID 该id 一般通过写cookie的方式 写入到客户端浏览器中。

tomcat中写cookie的name 一般默认为  JSESSIONID ,写入cookie 后 浏览器中可查看该cookie


会有一个路径 /weixin

然后客户端向服务端发送请求时会携带该cookie信息。

然后服务端根据cookie中的JSESSIONID的值从redis中获取存储的session信息。

那么问题来了  

这个路径 /weixin  和 /weixin/ 一样吗

显然不一样 

调试过程中 由于 前面提到的filter 没有拦截一些url 导致,一些请求产生了基于tomcat自己的session,没有从redis中获取。但是查看浏览器发现 JSESSIONID 这个cookie的存储路径 竟然为 /weixin/

然而其他拦截到的url 存的路径为 /weixin (开发人员自己设置的,获取的为contextpath)

也就是开发人员自己设置的 路径与 tomcat 自己设置的路径 不一样。这时调试环境的tomcat版本为8.0.X

我换成了 tomcat8.5.37时  cookie的路径竟然又变回了  /weixin

什么情况这是 。。。。。。。。

2.问题定位

为了一探究竟 我自己查看了 tomcat 设置cookie的路径的地方。

在 ApplicationSessionCookieConfig.java 类中 创建cookie的地方

/**

    * Creates a new session cookie for the given session ID

    *

    * @param context    The Context for the web application

    * @param sessionId  The ID of the session for which the cookie will be

    *                    created

    * @param secure      Should session cookie be configured as secure

    * @return the cookie for the session

    */

    public static Cookie createSessionCookie(Context context,

            String sessionId, boolean secure) {

        SessionCookieConfig scc =

            context.getServletContext().getSessionCookieConfig();

        // NOTE: The priority order for session cookie configuration is:

        //      1. Context level configuration

        //      2. Values from SessionCookieConfig

        //      3. Defaults

        Cookie cookie = new Cookie(

                SessionConfig.getSessionCookieName(context), sessionId);

        // Just apply the defaults.

        cookie.setMaxAge(scc.getMaxAge());

        cookie.setComment(scc.getComment());

        if (context.getSessionCookieDomain() == null) {

            // Avoid possible NPE

            if (scc.getDomain() != null) {

                cookie.setDomain(scc.getDomain());

            }

        } else {

            cookie.setDomain(context.getSessionCookieDomain());

        }

        // Always set secure if the request is secure

        if (scc.isSecure() || secure) {

            cookie.setSecure(true);

        }

        // Always set httpOnly if the context is configured for that

        if (scc.isHttpOnly() || context.getUseHttpOnly()) {

            cookie.setHttpOnly(true);

        }

        cookie.setPath(SessionConfig.getSessionCookiePath(context));

        return cookie;

    }


发现是从SessionConfig.java  获取的path ,然后继续贴代码

public static String getSessionCookiePath(Context context) {

        SessionCookieConfig scc = context.getServletContext().getSessionCookieConfig();

        String contextPath = context.getSessionCookiePath();

        if (contextPath == null || contextPath.length() == 0) {

            contextPath = scc.getPath();

        }

        if (contextPath == null || contextPath.length() == 0) {

            contextPath = context.getEncodedPath();

        }

        if (context.getSessionCookiePathUsesTrailingSlash()) {

            // Handle special case of ROOT context where cookies require a path of

            // '/' but the servlet spec uses an empty string

            // Also ensure the cookies for a context with a path of /foo don't get

            // sent for requests with a path of /foobar

            if (!contextPath.endsWith("/")) {

                contextPath = contextPath + "/";

            }

        } else {

            // Only handle special case of ROOT context where cookies require a

            // path of '/' but the servlet spec uses an empty string

            if (contextPath.length() == 0) {

                contextPath = "/";

            }

        }

        return contextPath;

    }

发现 其根据 SessionCookiePathUsesTrailingSlash 配置 来确定是否在  contextPath 后面加 “/” 

难道问题出在 这个属性的设置上?

然后我就查看tomcat的更新日志 果然查到了呢 哈哈哈哈哈哈

https://tomcat.apache.org/tomcat-8.5-doc/changelog.html

在8.5.4的版本中  有更新的说明如下 

Change the default of the sessionCookiePathUsesTrailingSlash attribute of the Context element to false since the problems caused when a Servlet is mapped to /* are more significant than the security risk of not enabling this option by default. (markt)

也就是 之前这个属性为 true 之后版本默认值改成了false 

所以 cookie的路径 返回  一个 为 /weixin/   一个为/weixin

至于为什么要修改呢

检索到这么一篇文章

http://tomcat.10.x6.nabble.com/Problems-with-default-for-sessionCookiePathUsesTrailingSlash-td5051868.html

从以上文章中可以学到  cookie 的path的作用呢 。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容