blob: 0ccbe22f02591f4b0c199f2aa4e66e9bbf70988e [file] [log] [blame]
<?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>DefaultWebSessionStorageEvaluator.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 :: Web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.mgt</a> &gt; <span class="el_source">DefaultWebSessionStorageEvaluator.java</span></div><h1>DefaultWebSessionStorageEvaluator.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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.mgt;
import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.session.mgt.NativeSessionManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.subject.WebSubject;
import org.apache.shiro.web.util.WebUtils;
/**
* A web-specific {@code SessionStorageEvaluator} that performs the same logic as the parent class
* {@link DefaultSessionStorageEvaluator} but additionally checks for a request-specific flag that may enable or
* disable session access.
* &lt;p/&gt;
* This implementation usually works in conjunction with the
* {@link org.apache.shiro.web.filter.session.NoSessionCreationFilter}: If the {@code NoSessionCreationFilter}
* is configured in a filter chain, that filter will set a specific
* {@code ServletRequest} {@link javax.servlet.ServletRequest#setAttribute attribute} indicating that session creation
* should be disabled.
* &lt;p/&gt;
* This {@code DefaultWebSessionStorageEvaluator} will then inspect this attribute, and if it has been set, will return
* {@code false} from {@link #isSessionStorageEnabled(org.apache.shiro.subject.Subject)} method, thereby preventing
* Shiro from creating a session for the purpose of storing subject state.
* &lt;p/&gt;
* If the request attribute has
* not been set (i.e. the {@code NoSessionCreationFilter} is not configured or has been disabled), this class does
* nothing and delegates to the parent class for existing behavior.
*
* @since 1.2
*/
<span class="fc" id="L49">public class DefaultWebSessionStorageEvaluator extends DefaultSessionStorageEvaluator {</span>
//since 1.2.1
private SessionManager sessionManager;
/**
* Sets the session manager to use when checking to see if session storage is possible.
* @param sessionManager the session manager instance for checking.
* @since 1.2.1
*/
//package protected on purpose to maintain point-version compatibility: (1.2.3 -&gt; 1.2.1 should work always).
void setSessionManager(SessionManager sessionManager) {
<span class="fc" id="L61"> this.sessionManager = sessionManager;</span>
<span class="fc" id="L62"> }</span>
/**
* Returns {@code true} if session storage is generally available (as determined by the super class's global
* configuration property {@link #isSessionStorageEnabled()} and no request-specific override has turned off
* session storage, {@code false} otherwise.
* &lt;p/&gt;
* This means session storage is disabled if the {@link #isSessionStorageEnabled()} property is {@code false} or if
* a request attribute is discovered that turns off session storage for the current request.
*
* @param subject the {@code Subject} for which session state persistence may be enabled
* @return {@code true} if session storage is generally available (as determined by the super class's global
* configuration property {@link #isSessionStorageEnabled()} and no request-specific override has turned off
* session storage, {@code false} otherwise.
*/
@SuppressWarnings({&quot;SimplifiableIfStatement&quot;})
@Override
public boolean isSessionStorageEnabled(Subject subject) {
<span class="fc bfc" id="L80" title="All 2 branches covered."> if (subject.getSession(false) != null) {</span>
//use what already exists
<span class="fc" id="L82"> return true;</span>
}
<span class="fc bfc" id="L85" title="All 2 branches covered."> if (!isSessionStorageEnabled()) {</span>
//honor global setting:
<span class="fc" id="L87"> return false;</span>
}
//SHIRO-350: non-web subject instances can't be saved to web-only session managers:
//since 1.2.1:
<span class="pc bpc" id="L92" title="1 of 6 branches missed."> if (!(subject instanceof WebSubject) &amp;&amp; (this.sessionManager != null &amp;&amp; !(this.sessionManager instanceof NativeSessionManager))) {</span>
<span class="fc" id="L93"> return false;</span>
}
<span class="fc" id="L96"> return WebUtils._isSessionCreationEnabled(subject);</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>