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的。
    也就是说,Subjec.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 {
    <span class="kw">private</span> <span class="dt">final</span> DelegatingSubject owner;</br></br>
    
    <span class="kw">private</span> <span class="fu">StoppingAwareProxiedSession</span>(Session target, DelegatingSubject owningSubject) {</br>
        <span class="kw">super</span>(target);</br>
        owner = owningSubject;</br>
    }</br>
    
    <span class="kw">public</span> <span class="dt">void</span> <span class="fu">stop</span>() <span class="kw">throws</span> InvalidSessionException {</br>
        <span class="kw">super</span>.<span class="fu">stop</span>();</br>
        owner.<span class="fu">sessionStopped</span>();</br>
    }</br>
    

    }

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

  • 相关阅读:
    ACE admin 后台管理框架
    HTML5 respond.js 解决IE6~8的响应式布局问题
    JS/JQ常见兼容辅助插件
    DEDECMS之十 修改织梦链和文章的默认来源及作者
    怎么在网站嵌入百度搜索框
    【python进阶】深入理解系统进程1
    BugkuCTF SQL注入1
    BugkuCTF 域名解析
    BugkuCTF sql注入
    BugkuCTF web3
  • 原文地址:https://www.cnblogs.com/jpfss/p/8442550.html
Copyright © 2011-2022 走看看