前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >细说shiro之六:session管理

细说shiro之六:session管理

作者头像
编程随笔
发布2019-09-11 15:41:43
1.2K0
发布2019-09-11 15:41:43
举报
文章被收录于专栏:后端开发随笔后端开发随笔

官网:https://shiro.apache.org/

我们先来看一下shiro中关于Session和Session Manager的类图。

如上图所示,shiro自己定义了一个新的Session接口,用于统一操作接口,并通过SessionManager实现Session管理。 其中的3个实现类HttpServletSession,SimpleSession和StoppingAwareProxiedSession是我们经常需要打交道的。

HttpServletSession

首先,我们来看看org.apache.shiro.web.session.HttpServletSession的实现。

代码语言:javascript
复制
public HttpServletSession(HttpSession httpSession, String host) {
    if (httpSession == null) {
        String msg = "HttpSession constructor argument cannot be null.";
        throw new IllegalArgumentException(msg);
    }
    if (httpSession instanceof ShiroHttpSession) {
        String msg = "HttpSession constructor argument cannot be an instance of ShiroHttpSession.  This " +
                "is enforced to prevent circular dependencies and infinite loops.";
        throw new IllegalArgumentException(msg);
    }
    this.httpSession = httpSession;
    if (StringUtils.hasText(host)) {
        setHost(host);
    }
}

显然,HttpServletSession只是简单对javax.servlet.http.HttpSession进行了封装,即: 在Web应用程序中,所有对Session相关的操作最终都是对javax.servlet.http.HttpSession进行的。

通过对上述Subject.login()的时序图分析可以知道: 在Web应用程序中,Shiro确实是通过ServletContainerSessionManager获取到容器创建的HttpSession再封装为HttpServletSession的。 也就是说,Subject.login()登录成功后用户的认证信息实际上是保存在HttpSession中的。如果此时Web应用程序部署了多实例,必须要进行Session同步。 我们知道,SecurityManager是整个Shiro框架的核心控制器,在SpringMVC中集成Shiro时,就需要明确配置对应的SecurityManager。

代码语言:javascript
复制
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <!-- Single realm app.  If you have multiple realms, use the 'realms' property instead. -->
    <property name="realm" ref="myRealm" />
</bean>

而在org.apache.shiro.web.mgt.DefaultWebSecurityManager的实现中,使用的SessionManager就是ServletContainerSessionManager。

代码语言:javascript
复制
public DefaultWebSecurityManager() {
    super();
    ((DefaultSubjectDAO) this.subjectDAO).setSessionStorageEvaluator(new DefaultWebSessionStorageEvaluator());
    this.sessionMode = HTTP_SESSION_MODE;
    setSubjectFactory(new DefaultWebSubjectFactory());
    setRememberMeManager(new CookieRememberMeManager());
    setSessionManager(new ServletContainerSessionManager()); // 配置Session Manager
}

SimpleSession

shiro具备完善的Session管理机制,当在命令行程序中使用Shiro框架时,同样可以执行与Web应用程序一样的Session操作。 此时,Shiro实际上使用SimpleSession实现。

StoppingAwareProxiedSession

实际上,StoppingAwareProxiedSession仅仅是一个Session包装类,即: 无论是HttpServletSession还是SimpleSession,在执行Subject.login()时保存到Subject中的Session都是StoppingAwareProxiedSession对象。

代码语言:javascript
复制
private class StoppingAwareProxiedSession extends ProxiedSession {

    private final DelegatingSubject owner;

    private StoppingAwareProxiedSession(Session target, DelegatingSubject owningSubject) {
        super(target);
        owner = owningSubject;
    }

    public void stop() throws InvalidSessionException {
        super.stop();
        owner.sessionStopped();
    }
}

【参考】 https://shiro.apache.org/session-management.html

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • HttpServletSession
  • SimpleSession
  • StoppingAwareProxiedSession
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档