| <?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>DefaultSubjectContext.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 :: Test Coverage</a> > <a href="../index.html" class="el_bundle">shiro-core</a> > <a href="index.source.html" class="el_package">org.apache.shiro.subject.support</a> > <span class="el_source">DefaultSubjectContext.java</span></div><h1>DefaultSubjectContext.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.subject.support; |
| |
| import org.apache.shiro.SecurityUtils; |
| import org.apache.shiro.UnavailableSecurityManagerException; |
| import org.apache.shiro.authc.AuthenticationInfo; |
| import org.apache.shiro.authc.AuthenticationToken; |
| import org.apache.shiro.authc.HostAuthenticationToken; |
| 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.util.MapContext; |
| import org.apache.shiro.util.StringUtils; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import java.io.Serializable; |
| |
| /** |
| * Default implementation of the {@link SubjectContext} interface. Note that the getters and setters are not |
| * simple pass-through methods to an underlying attribute; the getters will employ numerous heuristics to acquire |
| * their data attribute as best as possible (for example, if {@link #getPrincipals} is invoked, if the principals aren't |
| * in the backing map, it might check to see if there is a subject or session in the map and attempt to acquire the |
| * principals from those objects). |
| * |
| * @since 1.0 |
| */ |
| public class DefaultSubjectContext extends MapContext implements SubjectContext { |
| |
| <span class="fc" id="L49"> private static final String SECURITY_MANAGER = DefaultSubjectContext.class.getName() + ".SECURITY_MANAGER";</span> |
| |
| <span class="fc" id="L51"> private static final String SESSION_ID = DefaultSubjectContext.class.getName() + ".SESSION_ID";</span> |
| |
| <span class="fc" id="L53"> private static final String AUTHENTICATION_TOKEN = DefaultSubjectContext.class.getName() + ".AUTHENTICATION_TOKEN";</span> |
| |
| <span class="fc" id="L55"> private static final String AUTHENTICATION_INFO = DefaultSubjectContext.class.getName() + ".AUTHENTICATION_INFO";</span> |
| |
| <span class="fc" id="L57"> private static final String SUBJECT = DefaultSubjectContext.class.getName() + ".SUBJECT";</span> |
| |
| <span class="fc" id="L59"> private static final String PRINCIPALS = DefaultSubjectContext.class.getName() + ".PRINCIPALS";</span> |
| |
| <span class="fc" id="L61"> private static final String SESSION = DefaultSubjectContext.class.getName() + ".SESSION";</span> |
| |
| <span class="fc" id="L63"> private static final String AUTHENTICATED = DefaultSubjectContext.class.getName() + ".AUTHENTICATED";</span> |
| |
| <span class="fc" id="L65"> private static final String HOST = DefaultSubjectContext.class.getName() + ".HOST";</span> |
| |
| <span class="fc" id="L67"> public static final String SESSION_CREATION_ENABLED = DefaultSubjectContext.class.getName() + ".SESSION_CREATION_ENABLED";</span> |
| |
| /** |
| * The session key that is used to store subject principals. |
| */ |
| <span class="fc" id="L72"> public static final String PRINCIPALS_SESSION_KEY = DefaultSubjectContext.class.getName() + "_PRINCIPALS_SESSION_KEY";</span> |
| |
| /** |
| * The session key that is used to store whether or not the user is authenticated. |
| */ |
| <span class="fc" id="L77"> public static final String AUTHENTICATED_SESSION_KEY = DefaultSubjectContext.class.getName() + "_AUTHENTICATED_SESSION_KEY";</span> |
| |
| <span class="fc" id="L79"> private static final transient Logger log = LoggerFactory.getLogger(DefaultSubjectContext.class);</span> |
| |
| public DefaultSubjectContext() { |
| <span class="fc" id="L82"> super();</span> |
| <span class="fc" id="L83"> }</span> |
| |
| public DefaultSubjectContext(SubjectContext ctx) { |
| <span class="fc" id="L86"> super(ctx);</span> |
| <span class="fc" id="L87"> }</span> |
| |
| public SecurityManager getSecurityManager() { |
| <span class="fc" id="L90"> return getTypedValue(SECURITY_MANAGER, SecurityManager.class);</span> |
| } |
| |
| public void setSecurityManager(SecurityManager securityManager) { |
| <span class="fc" id="L94"> nullSafePut(SECURITY_MANAGER, securityManager);</span> |
| <span class="fc" id="L95"> }</span> |
| |
| public SecurityManager resolveSecurityManager() { |
| <span class="fc" id="L98"> SecurityManager securityManager = getSecurityManager();</span> |
| <span class="pc bpc" id="L99" title="1 of 2 branches missed."> if (securityManager == null) {</span> |
| <span class="nc bnc" id="L100" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="nc" id="L101"> log.debug("No SecurityManager available in subject context map. " +</span> |
| "Falling back to SecurityUtils.getSecurityManager() lookup."); |
| } |
| try { |
| <span class="nc" id="L105"> securityManager = SecurityUtils.getSecurityManager();</span> |
| <span class="nc" id="L106"> } catch (UnavailableSecurityManagerException e) {</span> |
| <span class="nc bnc" id="L107" title="All 2 branches missed."> if (log.isDebugEnabled()) {</span> |
| <span class="nc" id="L108"> log.debug("No SecurityManager available via SecurityUtils. Heuristics exhausted.", e);</span> |
| } |
| <span class="nc" id="L110"> }</span> |
| } |
| <span class="fc" id="L112"> return securityManager;</span> |
| } |
| |
| public Serializable getSessionId() { |
| <span class="fc" id="L116"> return getTypedValue(SESSION_ID, Serializable.class);</span> |
| } |
| |
| public void setSessionId(Serializable sessionId) { |
| <span class="nc" id="L120"> nullSafePut(SESSION_ID, sessionId);</span> |
| <span class="nc" id="L121"> }</span> |
| |
| public Subject getSubject() { |
| <span class="fc" id="L124"> return getTypedValue(SUBJECT, Subject.class);</span> |
| } |
| |
| public void setSubject(Subject subject) { |
| <span class="fc" id="L128"> nullSafePut(SUBJECT, subject);</span> |
| <span class="fc" id="L129"> }</span> |
| |
| public PrincipalCollection getPrincipals() { |
| <span class="fc" id="L132"> return getTypedValue(PRINCIPALS, PrincipalCollection.class);</span> |
| } |
| |
| private static boolean isEmpty(PrincipalCollection pc) { |
| <span class="pc bpc" id="L136" title="1 of 4 branches missed."> return pc == null || pc.isEmpty();</span> |
| } |
| |
| public void setPrincipals(PrincipalCollection principals) { |
| <span class="pc bpc" id="L140" title="1 of 2 branches missed."> if (!isEmpty(principals)) {</span> |
| <span class="fc" id="L141"> put(PRINCIPALS, principals);</span> |
| } |
| <span class="fc" id="L143"> }</span> |
| |
| public PrincipalCollection resolvePrincipals() { |
| <span class="fc" id="L146"> PrincipalCollection principals = getPrincipals();</span> |
| |
| <span class="fc bfc" id="L148" title="All 2 branches covered."> if (isEmpty(principals)) {</span> |
| //check to see if they were just authenticated: |
| <span class="fc" id="L150"> AuthenticationInfo info = getAuthenticationInfo();</span> |
| <span class="fc bfc" id="L151" title="All 2 branches covered."> if (info != null) {</span> |
| <span class="fc" id="L152"> principals = info.getPrincipals();</span> |
| } |
| } |
| |
| <span class="fc bfc" id="L156" title="All 2 branches covered."> if (isEmpty(principals)) {</span> |
| <span class="fc" id="L157"> Subject subject = getSubject();</span> |
| <span class="pc bpc" id="L158" title="1 of 2 branches missed."> if (subject != null) {</span> |
| <span class="nc" id="L159"> principals = subject.getPrincipals();</span> |
| } |
| } |
| |
| <span class="fc bfc" id="L163" title="All 2 branches covered."> if (isEmpty(principals)) {</span> |
| //try the session: |
| <span class="fc" id="L165"> Session session = resolveSession();</span> |
| <span class="fc bfc" id="L166" title="All 2 branches covered."> if (session != null) {</span> |
| <span class="fc" id="L167"> principals = (PrincipalCollection) session.getAttribute(PRINCIPALS_SESSION_KEY);</span> |
| } |
| } |
| |
| <span class="fc" id="L171"> return principals;</span> |
| } |
| |
| |
| public Session getSession() { |
| <span class="fc" id="L176"> return getTypedValue(SESSION, Session.class);</span> |
| } |
| |
| public void setSession(Session session) { |
| <span class="fc" id="L180"> nullSafePut(SESSION, session);</span> |
| <span class="fc" id="L181"> }</span> |
| |
| public Session resolveSession() { |
| <span class="fc" id="L184"> Session session = getSession();</span> |
| <span class="fc bfc" id="L185" title="All 2 branches covered."> if (session == null) {</span> |
| //try the Subject if it exists: |
| <span class="fc" id="L187"> Subject existingSubject = getSubject();</span> |
| <span class="fc bfc" id="L188" title="All 2 branches covered."> if (existingSubject != null) {</span> |
| <span class="fc" id="L189"> session = existingSubject.getSession(false);</span> |
| } |
| } |
| <span class="fc" id="L192"> return session;</span> |
| } |
| |
| public boolean isSessionCreationEnabled() { |
| <span class="fc" id="L196"> Boolean val = getTypedValue(SESSION_CREATION_ENABLED, Boolean.class);</span> |
| <span class="pc bpc" id="L197" title="3 of 4 branches missed."> return val == null || val;</span> |
| } |
| |
| public void setSessionCreationEnabled(boolean enabled) { |
| <span class="nc" id="L201"> nullSafePut(SESSION_CREATION_ENABLED, enabled);</span> |
| <span class="nc" id="L202"> }</span> |
| |
| public boolean isAuthenticated() { |
| <span class="nc" id="L205"> Boolean authc = getTypedValue(AUTHENTICATED, Boolean.class);</span> |
| <span class="nc bnc" id="L206" title="All 4 branches missed."> return authc != null && authc;</span> |
| } |
| |
| public void setAuthenticated(boolean authc) { |
| <span class="fc" id="L210"> put(AUTHENTICATED, authc);</span> |
| <span class="fc" id="L211"> }</span> |
| |
| public boolean resolveAuthenticated() { |
| <span class="fc" id="L214"> Boolean authc = getTypedValue(AUTHENTICATED, Boolean.class);</span> |
| <span class="fc bfc" id="L215" title="All 2 branches covered."> if (authc == null) {</span> |
| //see if there is an AuthenticationInfo object. If so, the very presence of one indicates a successful |
| //authentication attempt: |
| <span class="fc" id="L218"> AuthenticationInfo info = getAuthenticationInfo();</span> |
| <span class="pc bpc" id="L219" title="1 of 2 branches missed."> authc = info != null;</span> |
| } |
| <span class="fc bfc" id="L221" title="All 2 branches covered."> if (!authc) {</span> |
| //fall back to a session check: |
| <span class="fc" id="L223"> Session session = resolveSession();</span> |
| <span class="fc bfc" id="L224" title="All 2 branches covered."> if (session != null) {</span> |
| <span class="fc" id="L225"> Boolean sessionAuthc = (Boolean) session.getAttribute(AUTHENTICATED_SESSION_KEY);</span> |
| <span class="pc bpc" id="L226" title="1 of 4 branches missed."> authc = sessionAuthc != null && sessionAuthc;</span> |
| } |
| } |
| |
| <span class="fc" id="L230"> return authc;</span> |
| } |
| |
| public AuthenticationInfo getAuthenticationInfo() { |
| <span class="fc" id="L234"> return getTypedValue(AUTHENTICATION_INFO, AuthenticationInfo.class);</span> |
| } |
| |
| public void setAuthenticationInfo(AuthenticationInfo info) { |
| <span class="fc" id="L238"> nullSafePut(AUTHENTICATION_INFO, info);</span> |
| <span class="fc" id="L239"> }</span> |
| |
| public AuthenticationToken getAuthenticationToken() { |
| <span class="fc" id="L242"> return getTypedValue(AUTHENTICATION_TOKEN, AuthenticationToken.class);</span> |
| } |
| |
| public void setAuthenticationToken(AuthenticationToken token) { |
| <span class="fc" id="L246"> nullSafePut(AUTHENTICATION_TOKEN, token);</span> |
| <span class="fc" id="L247"> }</span> |
| |
| public String getHost() { |
| <span class="fc" id="L250"> return getTypedValue(HOST, String.class);</span> |
| } |
| |
| public void setHost(String host) { |
| <span class="nc bnc" id="L254" title="All 2 branches missed."> if (StringUtils.hasText(host)) {</span> |
| <span class="nc" id="L255"> put(HOST, host);</span> |
| } |
| <span class="nc" id="L257"> }</span> |
| |
| public String resolveHost() { |
| <span class="fc" id="L260"> String host = getHost();</span> |
| |
| <span class="pc bpc" id="L262" title="1 of 2 branches missed."> if (host == null) {</span> |
| //check to see if there is an AuthenticationToken from which to retrieve it: |
| <span class="fc" id="L264"> AuthenticationToken token = getAuthenticationToken();</span> |
| <span class="fc bfc" id="L265" title="All 2 branches covered."> if (token instanceof HostAuthenticationToken) {</span> |
| <span class="fc" id="L266"> host = ((HostAuthenticationToken) token).getHost();</span> |
| } |
| } |
| |
| <span class="fc bfc" id="L270" title="All 2 branches covered."> if (host == null) {</span> |
| <span class="fc" id="L271"> Session session = resolveSession();</span> |
| <span class="fc bfc" id="L272" title="All 2 branches covered."> if (session != null) {</span> |
| <span class="fc" id="L273"> host = session.getHost();</span> |
| } |
| } |
| |
| <span class="fc" id="L277"> return host;</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> |