| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>ServletContainerSessionManager.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: All (aggregate jar)</a> > <a href="../index.html" class="el_bundle">shiro-web</a> > <a href="index.source.html" class="el_package">org.apache.shiro.web.session.mgt</a> > <span class="el_source">ServletContainerSessionManager.java</span></div><h1>ServletContainerSessionManager.java</h1><pre class="source lang-java linenums">/* |
| * Licensed to the Apache Software Foundation (ASF) under one |
| * or more contributor license agreements. See the NOTICE file |
| * distributed with this work for additional information |
| * regarding copyright ownership. The ASF licenses this file |
| * to you under the Apache License, Version 2.0 (the |
| * "License"); you may not use this file except in compliance |
| * with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, |
| * software distributed under the License is distributed on an |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| * KIND, either express or implied. See the License for the |
| * specific language governing permissions and limitations |
| * under the License. |
| */ |
| package org.apache.shiro.web.session.mgt; |
| |
| import org.apache.shiro.authz.AuthorizationException; |
| import org.apache.shiro.session.Session; |
| import org.apache.shiro.session.SessionException; |
| import org.apache.shiro.session.mgt.SessionContext; |
| import org.apache.shiro.session.mgt.SessionKey; |
| import org.apache.shiro.web.session.HttpServletSession; |
| import org.apache.shiro.web.util.WebUtils; |
| |
| import javax.servlet.ServletRequest; |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpSession; |
| |
| |
| /** |
| * SessionManager implementation providing {@link Session} implementations that are merely wrappers for the |
| * Servlet container's {@link HttpSession}. |
| * <p/> |
| * Despite its name, this implementation <em>does not</em> itself manage Sessions since the Servlet container |
| * provides the actual management support. This class mainly exists to 'impersonate' a regular Shiro |
| * {@code SessionManager} so it can be pluggable into a normal Shiro configuration in a pure web application. |
| * <p/> |
| * Note that because this implementation relies on the {@link HttpSession HttpSession}, it is only functional in a |
| * servlet container - it is not capable of supporting Sessions for any clients other than those using the HTTP |
| * protocol. |
| * <p/> |
| * Therefore, if you need {@code Session} support for heterogeneous clients (e.g. web browsers, |
| * RMI clients, etc), use the {@link DefaultWebSessionManager DefaultWebSessionManager} |
| * instead. The {@code DefaultWebSessionManager} supports both traditional web-based access as well as non web-based |
| * clients. |
| * |
| * @since 0.9 |
| * @see DefaultWebSessionManager |
| */ |
| public class ServletContainerSessionManager implements WebSessionManager { |
| |
| //TODO - complete JavaDoc |
| |
| //TODO - read session timeout value from web.xml |
| |
| <span class="fc" id="L60"> public ServletContainerSessionManager() {</span> |
| <span class="fc" id="L61"> }</span> |
| |
| public Session start(SessionContext context) throws AuthorizationException { |
| <span class="fc" id="L64"> return createSession(context);</span> |
| } |
| |
| public Session getSession(SessionKey key) throws SessionException { |
| <span class="fc bfc" id="L68" title="All 2 branches covered."> if (!WebUtils.isHttp(key)) {</span> |
| <span class="fc" id="L69"> String msg = "SessionKey must be an HTTP compatible implementation.";</span> |
| <span class="fc" id="L70"> throw new IllegalArgumentException(msg);</span> |
| } |
| |
| <span class="fc" id="L73"> HttpServletRequest request = WebUtils.getHttpRequest(key);</span> |
| |
| <span class="fc" id="L75"> Session session = null;</span> |
| |
| <span class="fc" id="L77"> HttpSession httpSession = request.getSession(false);</span> |
| <span class="fc bfc" id="L78" title="All 2 branches covered."> if (httpSession != null) {</span> |
| <span class="fc" id="L79"> session = createSession(httpSession, request.getRemoteHost());</span> |
| } |
| |
| <span class="fc" id="L82"> return session;</span> |
| } |
| |
| private String getHost(SessionContext context) { |
| <span class="fc" id="L86"> String host = context.getHost();</span> |
| <span class="fc bfc" id="L87" title="All 2 branches covered."> if (host == null) {</span> |
| <span class="fc" id="L88"> ServletRequest request = WebUtils.getRequest(context);</span> |
| <span class="pc bpc" id="L89" title="1 of 2 branches missed."> if (request != null) {</span> |
| <span class="fc" id="L90"> host = request.getRemoteHost();</span> |
| } |
| } |
| <span class="fc" id="L93"> return host;</span> |
| |
| } |
| |
| /** |
| * @since 1.0 |
| */ |
| protected Session createSession(SessionContext sessionContext) throws AuthorizationException { |
| <span class="fc bfc" id="L101" title="All 2 branches covered."> if (!WebUtils.isHttp(sessionContext)) {</span> |
| <span class="fc" id="L102"> String msg = "SessionContext must be an HTTP compatible implementation.";</span> |
| <span class="fc" id="L103"> throw new IllegalArgumentException(msg);</span> |
| } |
| |
| <span class="fc" id="L106"> HttpServletRequest request = WebUtils.getHttpRequest(sessionContext);</span> |
| |
| <span class="fc" id="L108"> HttpSession httpSession = request.getSession();</span> |
| |
| //SHIRO-240: DO NOT use the 'globalSessionTimeout' value here on the acquired session. |
| //see: https://issues.apache.org/jira/browse/SHIRO-240 |
| |
| <span class="fc" id="L113"> String host = getHost(sessionContext);</span> |
| |
| <span class="fc" id="L115"> return createSession(httpSession, host);</span> |
| } |
| |
| protected Session createSession(HttpSession httpSession, String host) { |
| <span class="fc" id="L119"> return new HttpServletSession(httpSession, host);</span> |
| } |
| |
| /** |
| * This implementation always delegates to the servlet container for sessions, so this method returns |
| * {@code true} always. |
| * |
| * @return {@code true} always |
| * @since 1.2 |
| */ |
| public boolean isServletContainerSessions() { |
| <span class="nc" id="L130"> return true;</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.8.3.201901230119</span></div></body></html> |