ServletContainerSessionManager类

ServletContainerSessionManager是实现WebSessionManager接口的类,主要用于管理HTTP会话。文章详细解析了其构造方法、根据sessionContext创建session、从request获取session、获取主机地址、创建session以及判断session是否由servletContainer管理等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值