zoukankan      html  css  js  c++  java
  • 细说shiro之六:session管理


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

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

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

    HttpServletSession

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

    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。

    <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。

    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对象。

    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

  • 相关阅读:
    如何drop有default constraint的column SQL
    Asp.net中文本框全选的实现
    [转载]找回被误删的VISTA“显示桌面”图标
    [转]深入讲解数据库中User和Schema的关系
    模拟键盘输入
    ASP.NET页面之间传递值的几种方法
    查询整个数据库中某个特定值所在的表和字段的方法
    .NET的GCHandler
    DateTime.ToString() Patterns
    VS 2008的Setup project项目添加卸载功能
  • 原文地址:https://www.cnblogs.com/nuccch/p/7911423.html
Copyright © 2011-2022 走看看