今天的笔记是为了搞清楚4个问题, 搞清楚这四个问题,那么我工作上的困难也就解决了。

1).sessionId是在什么地方生成的?

2).sessionId的生产规则是怎么样的?

3).sessionId存储在哪里?

4).sessionId可以如何获取?

它是在容器里面生成的, spingBoot中内嵌的tomcat生成sessionId的方式,在org.apache.catalina.util.StandardSessionIdGenerator.

如果能在cookie里面拿到JSESSIONID, 那么一切问题就可以迎刃而解. 

一.tomcat

当我们调用HttpServletRequest.getSession(true)时,这个参数 true 的意思是“如果当前请求还没有 Session,就创建一个新的”。

那 Tomcat 在背后为我们做了些什么呢?

HttpServletRequest 是一个接口,Tomcat 实现了这个接口,具体实现类是:org.apache.catalina.connector.Request。

通过这块逻辑处理的:

Context context = getContext(); if (context == null) {     return null; }

Manager manager = context.getManager(); if (manager == null) {     return null;       }

session = manager.createSession(sessionId); session.access();

从上面的代码可以看出,Request 对象中持有 Context 容器对象,而 Context 容器持有 Session 管理器 Manager,这样通过 Context 组件就能拿到 Manager 组件,最后由 Manager 组件来创建 Session。

因此最后还是到了 StandardManager,StandardManager 的父类叫 ManagerBase,这个 createSession 方法定义在 ManagerBase 中,StandardManager 直接重用这个方法。

接着我们来看 ManagerBase 的 createSession 是如何实现的:

@Override public Session createSession(String sessionId) {     //首先判断Session数量是不是到了最大值,最大Session数可以通过参数设置     if ((maxActiveSessions >= 0) &&             (getActiveSessions() >= maxActiveSessions)) {         rejectedSessions++;         throw new TooManyActiveSessionsException(                 sm.getString("managerBase.createSession.ise"),                 maxActiveSessions);     }

    // 重用或者创建一个新的Session对象,请注意在Tomcat中就是StandardSession     // 它是HttpSession的具体实现类,而HttpSession是Servlet规范中定义的接口     Session session = createEmptySession();

    // 初始化新Session的值     session.setNew(true);     session.setValid(true);     session.setCreationTime(System.currentTimeMillis());     session.setMaxInactiveInterval(getContext().getSessionTimeout() * 60);     String id = sessionId;     if (id == null) {         id = generateSessionId();     }     session.setId(id);// 这里会将Session添加到ConcurrentHashMap中     sessionCounter++;          //将创建时间添加到LinkedList中,并且把最先添加的时间移除     //主要还是方便清理过期Session     SessionTiming timing = new SessionTiming(session.getCreationTime(), 0);     synchronized (sessionCreationTiming) {         sessionCreationTiming.add(timing);         sessionCreationTiming.poll();     }     return session } 到此我们明白了 Session 是如何创建出来的,创建出来后 Session 会被保存到一个 ConcurrentHashMap 中:

protected Map sessions = new ConcurrentHashMap<>(); 请注意 Session 的具体实现类是 StandardSession,StandardSession 同时实现了javax.servlet.http.HttpSession和org.apache.catalina.Session接口,并且对程序员暴露的是 StandardSessionFacade 外观类,保证了 StandardSession 的安全,避免了程序员调用其内部方法进行不当操作。  

二.jetty

SessionCache的基本默认实现是DefaultSessionCache,里面存储的session都是用ConcurrentHashMap存储的,key是sessionid,value是session对象:

此时我们只需要能得到这个SessionHandler,就能通过id获取我们想要的session啦!

我发现org.eclipse.jetty.server.Request有个getSessionHandler方法,正好符合我们的预期:

先获取SessionHandler -> id -> session, 思路大概是这么个思路.

简要的思路大概就是这么个思路.

参考文章

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。