blob: e2db7d30a39dbfb0e88f85d094f83905f71ebe32 [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>DefaultWebSecurityManager.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> &gt; <a href="../index.html" class="el_bundle">shiro-web</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.web.mgt</a> &gt; <span class="el_source">DefaultWebSecurityManager.java</span></div><h1>DefaultWebSecurityManager.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.DefaultSecurityManager;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SessionStorageEvaluator;
import org.apache.shiro.mgt.SubjectDAO;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.util.LifecycleUtils;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.apache.shiro.web.session.mgt.*;
import org.apache.shiro.web.subject.WebSubject;
import org.apache.shiro.web.subject.WebSubjectContext;
import org.apache.shiro.web.subject.support.DefaultWebSubjectContext;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
import java.util.Collection;
/**
* Default {@link WebSecurityManager WebSecurityManager} implementation used in web-based applications or any
* application that requires HTTP connectivity (SOAP, http remoting, etc).
*
* @since 0.2
*/
public class DefaultWebSecurityManager extends DefaultSecurityManager implements WebSecurityManager {
//TODO - complete JavaDoc
<span class="fc" id="L57"> private static final Logger log = LoggerFactory.getLogger(DefaultWebSecurityManager.class);</span>
@Deprecated
public static final String HTTP_SESSION_MODE = &quot;http&quot;;
@Deprecated
public static final String NATIVE_SESSION_MODE = &quot;native&quot;;
/**
* @deprecated as of 1.2. This should NOT be used for anything other than determining if the sessionMode has changed.
*/
@Deprecated
private String sessionMode;
public DefaultWebSecurityManager() {
<span class="fc" id="L71"> super();</span>
<span class="fc" id="L72"> DefaultWebSessionStorageEvaluator webEvalutator = new DefaultWebSessionStorageEvaluator(); </span>
<span class="fc" id="L73"> ((DefaultSubjectDAO) this.subjectDAO).setSessionStorageEvaluator(webEvalutator);</span>
<span class="fc" id="L74"> this.sessionMode = HTTP_SESSION_MODE;</span>
<span class="fc" id="L75"> setSubjectFactory(new DefaultWebSubjectFactory());</span>
<span class="fc" id="L76"> setRememberMeManager(new CookieRememberMeManager());</span>
<span class="fc" id="L77"> setSessionManager(new ServletContainerSessionManager());</span>
<span class="fc" id="L78"> webEvalutator.setSessionManager(getSessionManager());</span>
<span class="fc" id="L79"> }</span>
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
public DefaultWebSecurityManager(Realm singleRealm) {
<span class="nc" id="L83"> this();</span>
<span class="nc" id="L84"> setRealm(singleRealm);</span>
<span class="nc" id="L85"> }</span>
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
public DefaultWebSecurityManager(Collection&lt;Realm&gt; realms) {
<span class="fc" id="L89"> this();</span>
<span class="fc" id="L90"> setRealms(realms);</span>
<span class="fc" id="L91"> }</span>
@Override
protected SubjectContext createSubjectContext() {
<span class="fc" id="L95"> return new DefaultWebSubjectContext();</span>
}
@Override
//since 1.2.1 for fixing SHIRO-350
public void setSubjectDAO(SubjectDAO subjectDAO) {
<span class="fc" id="L101"> super.setSubjectDAO(subjectDAO);</span>
<span class="fc" id="L102"> applySessionManagerToSessionStorageEvaluatorIfPossible();</span>
<span class="fc" id="L103"> }</span>
//since 1.2.1 for fixing SHIRO-350
@Override
protected void afterSessionManagerSet() {
<span class="fc" id="L108"> super.afterSessionManagerSet();</span>
<span class="fc" id="L109"> applySessionManagerToSessionStorageEvaluatorIfPossible();</span>
<span class="fc" id="L110"> }</span>
//since 1.2.1 for fixing SHIRO-350:
private void applySessionManagerToSessionStorageEvaluatorIfPossible() {
<span class="fc" id="L114"> SubjectDAO subjectDAO = getSubjectDAO();</span>
<span class="pc bpc" id="L115" title="1 of 2 branches missed."> if (subjectDAO instanceof DefaultSubjectDAO) {</span>
<span class="fc" id="L116"> SessionStorageEvaluator evaluator = ((DefaultSubjectDAO)subjectDAO).getSessionStorageEvaluator();</span>
<span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (evaluator instanceof DefaultWebSessionStorageEvaluator) {</span>
<span class="fc" id="L118"> ((DefaultWebSessionStorageEvaluator)evaluator).setSessionManager(getSessionManager());</span>
}
}
<span class="fc" id="L121"> }</span>
@Override
protected SubjectContext copy(SubjectContext subjectContext) {
<span class="fc bfc" id="L125" title="All 2 branches covered."> if (subjectContext instanceof WebSubjectContext) {</span>
<span class="fc" id="L126"> return new DefaultWebSubjectContext((WebSubjectContext) subjectContext);</span>
}
<span class="fc" id="L128"> return super.copy(subjectContext);</span>
}
@SuppressWarnings({&quot;UnusedDeclaration&quot;})
@Deprecated
public String getSessionMode() {
<span class="nc" id="L134"> return sessionMode;</span>
}
/**
* @param sessionMode
* @deprecated since 1.2
*/
@Deprecated
public void setSessionMode(String sessionMode) {
<span class="fc" id="L143"> log.warn(&quot;The 'sessionMode' property has been deprecated. Please configure an appropriate WebSessionManager &quot; +</span>
&quot;instance instead of using this property. This property/method will be removed in a later version.&quot;);
<span class="fc" id="L145"> String mode = sessionMode;</span>
<span class="pc bpc" id="L146" title="1 of 2 branches missed."> if (mode == null) {</span>
<span class="nc" id="L147"> throw new IllegalArgumentException(&quot;sessionMode argument cannot be null.&quot;);</span>
}
<span class="fc" id="L149"> mode = sessionMode.toLowerCase();</span>
<span class="pc bpc" id="L150" title="2 of 4 branches missed."> if (!HTTP_SESSION_MODE.equals(mode) &amp;&amp; !NATIVE_SESSION_MODE.equals(mode)) {</span>
<span class="nc" id="L151"> String msg = &quot;Invalid sessionMode [&quot; + sessionMode + &quot;]. Allowed values are &quot; +</span>
<span class="nc" id="L152"> &quot;public static final String constants in the &quot; + getClass().getName() + &quot; class: '&quot;</span>
+ HTTP_SESSION_MODE + &quot;' or '&quot; + NATIVE_SESSION_MODE + &quot;', with '&quot; +
HTTP_SESSION_MODE + &quot;' being the default.&quot;;
<span class="nc" id="L155"> throw new IllegalArgumentException(msg);</span>
}
<span class="pc bpc" id="L157" title="1 of 4 branches missed."> boolean recreate = this.sessionMode == null || !this.sessionMode.equals(mode);</span>
<span class="fc" id="L158"> this.sessionMode = mode;</span>
<span class="fc bfc" id="L159" title="All 2 branches covered."> if (recreate) {</span>
<span class="fc" id="L160"> LifecycleUtils.destroy(getSessionManager());</span>
<span class="fc" id="L161"> SessionManager sessionManager = createSessionManager(mode);</span>
<span class="fc" id="L162"> this.setInternalSessionManager(sessionManager);</span>
}
<span class="fc" id="L164"> }</span>
@Override
public void setSessionManager(SessionManager sessionManager) {
<span class="fc" id="L168"> this.sessionMode = null;</span>
<span class="pc bpc" id="L169" title="2 of 4 branches missed."> if (sessionManager != null &amp;&amp; !(sessionManager instanceof WebSessionManager)) {</span>
<span class="nc bnc" id="L170" title="All 2 branches missed."> if (log.isWarnEnabled()) {</span>
<span class="nc" id="L171"> String msg = &quot;The &quot; + getClass().getName() + &quot; implementation expects SessionManager instances &quot; +</span>
<span class="nc" id="L172"> &quot;that implement the &quot; + WebSessionManager.class.getName() + &quot; interface. The &quot; +</span>
<span class="nc" id="L173"> &quot;configured instance is of type [&quot; + sessionManager.getClass().getName() + &quot;] which does not &quot; +</span>
&quot;implement this interface.. This may cause unexpected behavior.&quot;;
<span class="nc" id="L175"> log.warn(msg);</span>
}
}
<span class="fc" id="L178"> setInternalSessionManager(sessionManager);</span>
<span class="fc" id="L179"> }</span>
/**
* @param sessionManager
* @since 1.2
*/
private void setInternalSessionManager(SessionManager sessionManager) {
<span class="fc" id="L186"> super.setSessionManager(sessionManager);</span>
<span class="fc" id="L187"> }</span>
/**
* @since 1.0
*/
public boolean isHttpSessionMode() {
<span class="fc" id="L193"> SessionManager sessionManager = getSessionManager();</span>
<span class="pc bpc" id="L194" title="1 of 4 branches missed."> return sessionManager instanceof WebSessionManager &amp;&amp; ((WebSessionManager)sessionManager).isServletContainerSessions();</span>
}
protected SessionManager createSessionManager(String sessionMode) {
<span class="pc bpc" id="L198" title="2 of 4 branches missed."> if (sessionMode == null || !sessionMode.equalsIgnoreCase(NATIVE_SESSION_MODE)) {</span>
<span class="nc" id="L199"> log.info(&quot;{} mode - enabling ServletContainerSessionManager (HTTP-only Sessions)&quot;, HTTP_SESSION_MODE);</span>
<span class="nc" id="L200"> return new ServletContainerSessionManager();</span>
} else {
<span class="fc" id="L202"> log.info(&quot;{} mode - enabling DefaultWebSessionManager (non-HTTP and HTTP Sessions)&quot;, NATIVE_SESSION_MODE);</span>
<span class="fc" id="L203"> return new DefaultWebSessionManager();</span>
}
}
@Override
protected SessionContext createSessionContext(SubjectContext subjectContext) {
<span class="nc" id="L209"> SessionContext sessionContext = super.createSessionContext(subjectContext);</span>
<span class="nc bnc" id="L210" title="All 2 branches missed."> if (subjectContext instanceof WebSubjectContext) {</span>
<span class="nc" id="L211"> WebSubjectContext wsc = (WebSubjectContext) subjectContext;</span>
<span class="nc" id="L212"> ServletRequest request = wsc.resolveServletRequest();</span>
<span class="nc" id="L213"> ServletResponse response = wsc.resolveServletResponse();</span>
<span class="nc" id="L214"> DefaultWebSessionContext webSessionContext = new DefaultWebSessionContext(sessionContext);</span>
<span class="nc bnc" id="L215" title="All 2 branches missed."> if (request != null) {</span>
<span class="nc" id="L216"> webSessionContext.setServletRequest(request);</span>
}
<span class="nc bnc" id="L218" title="All 2 branches missed."> if (response != null) {</span>
<span class="nc" id="L219"> webSessionContext.setServletResponse(response);</span>
}
<span class="nc" id="L222"> sessionContext = webSessionContext;</span>
}
<span class="nc" id="L224"> return sessionContext;</span>
}
@Override
protected SessionKey getSessionKey(SubjectContext context) {
<span class="fc bfc" id="L229" title="All 2 branches covered."> if (WebUtils.isWeb(context)) {</span>
<span class="fc" id="L230"> Serializable sessionId = context.getSessionId();</span>
<span class="fc" id="L231"> ServletRequest request = WebUtils.getRequest(context);</span>
<span class="fc" id="L232"> ServletResponse response = WebUtils.getResponse(context);</span>
<span class="fc" id="L233"> return new WebSessionKey(sessionId, request, response);</span>
} else {
<span class="fc" id="L235"> return super.getSessionKey(context);</span>
}
}
@Override
protected void beforeLogout(Subject subject) {
<span class="nc" id="L242"> super.beforeLogout(subject);</span>
<span class="nc" id="L243"> removeRequestIdentity(subject);</span>
<span class="nc" id="L244"> }</span>
protected void removeRequestIdentity(Subject subject) {
<span class="nc bnc" id="L247" title="All 2 branches missed."> if (subject instanceof WebSubject) {</span>
<span class="nc" id="L248"> WebSubject webSubject = (WebSubject) subject;</span>
<span class="nc" id="L249"> ServletRequest request = webSubject.getServletRequest();</span>
<span class="nc bnc" id="L250" title="All 2 branches missed."> if (request != null) {</span>
<span class="nc" id="L251"> request.setAttribute(ShiroHttpServletRequest.IDENTITY_REMOVED_KEY, Boolean.TRUE);</span>
}
}
<span class="nc" id="L254"> }</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>