//获取当前用户Subject subject = SecurityUtils.getSubject();//获取当前用户session,默认为不存在session就创建sessionSession session = subject.getSessio();而subject的实现类通过打断点可以知道其实现类为DelegatingSubject,而他有一个方法叫getSession(boolean create),一看就知道是获取session的意思,这个方法里有一段代码:Session session = this.securityManager.start(sessionContext);这个this.securityManager的是实现类是ServletContainerSessionManager,查看他的start方法: Session session = this.securityManager.start(sessionContext);其实就是包含了一个createSession方法,然后看HttpSession httpSession = request.getSession();这段代码,原来shiro获取session还是依赖着httpSession的。 protected Session createSession(SessionContext sessionContext) throws AuthorizationException { if (!WebUtils.isHttp(sessionContext)) { String msg = "SessionContext must be an HTTP compatible implementation."; throw new IllegalArgumentException(msg); } HttpServletRequest request = WebUtils.getHttpRequest(sessionContext); HttpSession httpSession = request.getSession(); //SHIRO-240: DO NOT use the 'globalSessionTimeout' value here on the acquired session. //see: https://issues.apache.org/jira/browse/SHIRO-240 String host = getHost(sessionContext); return createSession(httpSession, host); }
继续往下想,request是如何获取session的。。个人认为是tomcat实现的