HttpShiroRequest 接管session

來源:互聯網
上載者:User

標籤:

簡單分析完jetty的請求處理過程:http://my.oschina.net/u/782865/blog/505533

來看看shiro是怎麼接管session的;我們知道web容器都實現有session管理;要把這個容器預設實現屏蔽掉,就需要改變request的getSession方法的預設實現;

shiro是這樣實現的:封裝普通的request成一個wrap,即是ShiroHttpServletRequest;

通過攔截器攔截request並封裝為ShiroHttpServletRequest;具體如下

shiro的AbstractShiroFilter

protected void doFilterInternal(ServletRequest servletRequest, ServletResponse servletResponse, final FilterChain chain)            throws ServletException, IOException {        Throwable t = null;        try {            final ServletRequest request = prepareServletRequest(servletRequest, servletResponse, chain);//執行封裝            final ServletResponse response = prepareServletResponse(request, servletResponse, chain);            final Subject subject = createSubject(request, response);            //noinspection unchecked            subject.execute(new Callable() {                public Object call() throws Exception {                    updateSessionLastAccessTime(request, response);                    executeChain(request, response, chain);                    return null;                }            });...
@SuppressWarnings({"UnusedDeclaration"})    protected ServletRequest prepareServletRequest(ServletRequest request, ServletResponse response, FilterChain chain) {        ServletRequest toUse = request;        if (request instanceof HttpServletRequest) {            HttpServletRequest http = (HttpServletRequest) request;            toUse = wrapServletRequest(http);        }        return toUse;    }
protected ServletRequest wrapServletRequest(HttpServletRequest orig) {        return new ShiroHttpServletRequest(orig, getServletContext(), isHttpSessions());    }

預設不用容器的session實現;

再來看看ShiroHttpServletRequest的getSession方法;

public HttpSession getSession() {        return getSession(true);    }
public HttpSession getSession(boolean create) {        HttpSession httpSession;        if (isHttpSessions()) {//預設是flase;            httpSession = super.getSession(false);            if (httpSession == null && create) {                //Shiro 1.2: assert that creation is enabled (SHIRO-266):                if (WebUtils._isSessionCreationEnabled(this)) {                    httpSession = super.getSession(create);                } else {                    throw newNoSessionCreationException();                }            }        } else {//這裡是shiro實現的session            if (this.session == null) {                boolean existing = getSubject().getSession(false) != null;                Session shiroSession = getSubject().getSession(create);                if (shiroSession != null) {                    this.session = new ShiroHttpSession(shiroSession, this, this.servletContext);                    if (!existing) {                        setAttribute(REFERENCED_SESSION_IS_NEW, Boolean.TRUE);                    }                }            }            httpSession = this.session;        }        return httpSession;    }

那麼實現shiro的sessionDao介面就可以想存哪就存哪了;

HttpShiroRequest 接管session

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.