| <?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>DefaultWebSubjectFactory.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.mgt</a> > <span class="el_source">DefaultWebSubjectFactory.java</span></div><h1>DefaultWebSubjectFactory.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.mgt; |
| |
| import org.apache.shiro.mgt.DefaultSubjectFactory; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.session.Session; |
| import org.apache.shiro.subject.PrincipalCollection; |
| import org.apache.shiro.subject.Subject; |
| import org.apache.shiro.subject.SubjectContext; |
| import org.apache.shiro.web.subject.WebSubjectContext; |
| import org.apache.shiro.web.subject.support.WebDelegatingSubject; |
| |
| import javax.servlet.ServletRequest; |
| import javax.servlet.ServletResponse; |
| import org.apache.shiro.web.subject.WebSubject; |
| |
| /** |
| * A {@code SubjectFactory} implementation that creates {@link WebDelegatingSubject} instances. |
| * <p/> |
| * {@code WebDelegatingSubject} instances are required if Request/Response objects are to be maintained across |
| * threads when using the {@code Subject} {@link Subject#associateWith(java.util.concurrent.Callable) createCallable} |
| * and {@link Subject#associateWith(Runnable) createRunnable} methods. |
| * |
| * @since 1.0 |
| */ |
| public class DefaultWebSubjectFactory extends DefaultSubjectFactory { |
| |
| public DefaultWebSubjectFactory() { |
| <span class="fc" id="L46"> super();</span> |
| <span class="fc" id="L47"> }</span> |
| |
| public Subject createSubject(SubjectContext context) { |
| //SHIRO-646 |
| //Check if the existing subject is NOT a WebSubject. If it isn't, then call super.createSubject instead. |
| //Creating a WebSubject from a non-web Subject will cause the ServletRequest and ServletResponse to be null, which wil fail when creating a session. |
| <span class="fc bfc" id="L53" title="All 4 branches covered."> boolean isNotBasedOnWebSubject = context.getSubject() != null && !(context.getSubject() instanceof WebSubject);</span> |
| <span class="fc bfc" id="L54" title="All 4 branches covered."> if (!(context instanceof WebSubjectContext) || isNotBasedOnWebSubject) {</span> |
| <span class="fc" id="L55"> return super.createSubject(context);</span> |
| } |
| <span class="fc" id="L57"> WebSubjectContext wsc = (WebSubjectContext) context;</span> |
| <span class="fc" id="L58"> SecurityManager securityManager = wsc.resolveSecurityManager();</span> |
| <span class="fc" id="L59"> Session session = wsc.resolveSession();</span> |
| <span class="fc" id="L60"> boolean sessionEnabled = wsc.isSessionCreationEnabled();</span> |
| <span class="fc" id="L61"> PrincipalCollection principals = wsc.resolvePrincipals();</span> |
| <span class="fc" id="L62"> boolean authenticated = wsc.resolveAuthenticated();</span> |
| <span class="fc" id="L63"> String host = wsc.resolveHost();</span> |
| <span class="fc" id="L64"> ServletRequest request = wsc.resolveServletRequest();</span> |
| <span class="fc" id="L65"> ServletResponse response = wsc.resolveServletResponse();</span> |
| |
| <span class="fc" id="L67"> return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled,</span> |
| request, response, securityManager); |
| } |
| |
| /** |
| * @deprecated since 1.2 - override {@link #createSubject(org.apache.shiro.subject.SubjectContext)} directly if you |
| * need to instantiate a custom {@link Subject} class. |
| */ |
| @Deprecated |
| protected Subject newSubjectInstance(PrincipalCollection principals, boolean authenticated, |
| String host, Session session, |
| ServletRequest request, ServletResponse response, |
| SecurityManager securityManager) { |
| <span class="nc" id="L80"> return new WebDelegatingSubject(principals, authenticated, host, session, true,</span> |
| request, response, securityManager); |
| } |
| } |
| </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> |