shiro session HttpSession servlet session

Shiro 提供了三个默认实现:

  1. DefaultSessionManager:DefaultSecurityManager 使用的默认实现,用于 JavaSE 环境;
  2. ServletContainerSessionManager:DefaultWebSecurityManager 使用的默认实现,用于 Web环境,其直接使用 Servlet 容器的会话;
  3. DefaultWebSessionManager : 用 于 Web 环 境 的 实 现 , 可 以 替 代ServletContainerSessionManager,自己维护着会话,直接废弃了 Servlet 容器的会话管理

我的驾驶舱项目中用的默认的ServletContainerSessionManager,在 Servlet 容器中,默认使用 JSESSIONID Cookie 维护会话,且会话默认是跟容器绑定的

org.apache.shiro.web.session.HttpServletSession继承org.apache.shiro.session.Session,含有javax.servlet.http.HttpSession域org.apache.shiro.web.servlet.ShiroHttpSession继承javax.servlet.http.HttpSession;含有org.apache.shiro.session.Session域

所以,

  1. 要么是使用shiro默认创建的Session的子类对象HttpServletSession,其中包含了HttpSession信息
  2. 要么是使用者重新创建HttpSession的子类对象ShiroHttpSession,其中包含了shiro的Session信息

ServletContainerSessionManager对应HttpServletSession

DefaultWebSessionManager对应ShiroHttpSession



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

  上两段代码,是默认情况下,利用HttpSession 构造HttpServletSession;


    public ShiroHttpSession(Session session, HttpServletRequest currentRequest, ServletContext servletContext) {
        if (session instanceof HttpServletSession) {
            String msg = "Session constructor argument cannot be an instance of HttpServletSession.  This is enforced to " +
                    "prevent circular dependencies and infinite loops.";
            throw new IllegalArgumentException(msg);
        }
        this.session = session;
        this.currentRequest = currentRequest;
        this.servletContext = servletContext;
    }

 上述代码,利用shiro Session自建ShiroHttpSession(HttpSession)


package javax.servlet.http;

public interface HttpSession {

package org.apache.shiro.session;


public interface Session {

 

 

  

 

 

Shiro框架没有使用Tomcat的session,而是重新实现了一套自己的session机制。因此,当引入Shiro后,传统的Tomcat session共享机制将失效,必须采用面向Shirosession共享方式。 为了配置Shirosession共享,需要进行以下步骤: 1. 在shiro-web.ini配置文件中添加以下代码: ``` [main] sessionManager=org.apache.shiro.session.mgt.DefaultSessionManager securityManager.sessionManager=$sessionManager ``` 这样可以创建一个DefaultSessionManager的实例,并将其设置为securityManager的sessionManager属性值。 2. 在shiro-web.ini配置文件中继续添加以下代码: ``` [main] sessionFactory=org.apache.shiro.session.mgt.OnlineSessionFactory sessionManager.sessionFactory=$sessionFactory ``` 这样可以创建一个OnlineSessionFactory的实例,并将其设置为sessionManager的sessionFactory属性值。 通过以上配置,你就可以实现Shirosession共享了。你也可以在网上找到更多关于Shiro session共享的文章来进一步学习和了解。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [基于spring redis的shiro session共享](https://download.csdn.net/download/z0120409310507/9854349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Shiro学习(10)Session管理](https://blog.csdn.net/m0_67403073/article/details/126496325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值