ServletContainerSessionManager类主要定义了,它实现了WebSessionManager接口,现对其解析如下:
1.WebSessionManager接口
可以参考WebSessionManager接口源码解析,里面只有一个方法, 定义了session是否由servletcontainer管理。
2.ServletContainerSessionManager类
2.1.构造方法
public ServletContainerSessionManager() {
}
2.2.根据sessionContext创建session信息(它实现了WebSessionManager接口的方法)
public Session start(SessionContext context) throws AuthorizationException {
return createSession(context);
}
2.3.根据sessionKey从request中获取session信息(它实现了WebSessionManager接口的方法)
public Session getSession(SessionKey key) throws SessionException {
if (!WebUtils.isHttp(key)) {
String msg = "SessionKey must be an HTTP compatible implementation.";
throw new IllegalArgumentException(msg);
}
HttpServletRequest request = WebUtils.getHttpRequest(key);
Session session = null;
HttpSession httpSession = request.getSession(false);
if (httpSession != null) {
session = createSession(httpSession, request.getRemoteHost());
}
return session;
}
2.4.获取主机地址(先从session上下文中获取,如果为空,则从request中获取)
private String getHost(SessionContext context) {
String host = context.getHost();
if (host == null) {
ServletRequest request = WebUtils.getRequest(context);
if (request != null) {
host = request.getRemoteHost();
}
}
return host;
}
2.5.创建session
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);
}
2.6.创建session信息
protected Session createSession(HttpSession httpSession, String host) {
return new HttpServletSession(httpSession, host);
}
2.7.session是否由servletContainer管理
public boolean isServletContainerSessions() {
return true;
}