001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.shiro;
020
021import org.apache.shiro.mgt.SecurityManager;
022import org.apache.shiro.mgt.WrappedSecurityManager;
023import org.apache.shiro.subject.Subject;
024import org.apache.shiro.util.ThreadContext;
025import java.util.Objects;
026import java.util.function.Predicate;
027
028
029/**
030 * Accesses the currently accessible {@code Subject} for the calling code depending on runtime environment.
031 *
032 * @since 0.2
033 */
034public abstract class SecurityUtils {
035
036    /**
037     * ONLY used as a 'backup' in VM Singleton environments (that is, standalone environments), since the
038     * ThreadContext should always be the primary source for Subject instances when possible.
039     */
040    private static volatile SecurityManager securityManager;
041
042    /**
043     * Returns the currently accessible {@code Subject} available to the calling code depending on
044     * runtime environment.
045     * <p/>
046     * This method is provided as a way of obtaining a {@code Subject} without having to resort to
047     * implementation-specific methods.  It also allows the Shiro team to change the underlying implementation of
048     * this method in the future depending on requirements/updates without affecting your code that uses it.
049     *
050     * @return the currently accessible {@code Subject} accessible to the calling code.
051     * @throws IllegalStateException if no {@link Subject Subject} instance or
052     *                               {@link SecurityManager SecurityManager} instance is available with which to obtain
053     *                               a {@code Subject}, which which is considered an invalid application configuration
054     *                               - a Subject should <em>always</em> be available to the caller.
055     */
056    public static Subject getSubject() {
057        Subject subject = ThreadContext.getSubject();
058        if (subject == null) {
059            subject = (new Subject.Builder()).buildSubject();
060            ThreadContext.bind(subject);
061        }
062        return subject;
063    }
064
065    /**
066     * Sets a VM (static) singleton SecurityManager, specifically for transparent use in the
067     * {@link #getSubject() getSubject()} implementation.
068     * <p/>
069     * <b>This method call exists mainly for framework development support.  Application developers should rarely,
070     * if ever, need to call this method.</b>
071     * <p/>
072     * The Shiro development team prefers that SecurityManager instances are non-static application singletons
073     * and <em>not</em> VM static singletons.  Application singletons that do not use static memory require some sort
074     * of application configuration framework to maintain the application-wide SecurityManager instance for you
075     * (for example, Spring or EJB3 environments) such that the object reference does not need to be static.
076     * <p/>
077     * In these environments, Shiro acquires Subject data based on the currently executing Thread via its own
078     * framework integration code, and this is the preferred way to use Shiro.
079     * <p/>
080     * However in some environments, such as a standalone desktop application or Applets that do not use Spring or
081     * EJB or similar config frameworks, a VM-singleton might make more sense (although the former is still preferred).
082     * In these environments, setting the SecurityManager via this method will automatically enable the
083     * {@link #getSubject() getSubject()} call to function with little configuration.
084     * <p/>
085     * For example, in these environments, this will work:
086     * <pre>
087     * DefaultSecurityManager securityManager = new {@link org.apache.shiro.mgt.DefaultSecurityManager DefaultSecurityManager}();
088     * securityManager.setRealms( ... ); //one or more Realms
089     * <b>SecurityUtils.setSecurityManager( securityManager );</b></pre>
090     * <p/>
091     * And then anywhere in the application code, the following call will return the application's Subject:
092     * <pre>
093     * Subject currentUser = SecurityUtils.getSubject();</pre>
094     *
095     * @param securityManager the securityManager instance to set as a VM static singleton.
096     */
097    public static void setSecurityManager(SecurityManager securityManager) {
098        SecurityUtils.securityManager = securityManager;
099    }
100
101    /**
102     * Returns the SecurityManager accessible to the calling code.
103     * <p/>
104     * This implementation favors acquiring a thread-bound {@code SecurityManager} if it can find one.  If one is
105     * not available to the executing thread, it will attempt to use the static singleton if available (see the
106     * {@link #setSecurityManager setSecurityManager} method for more on the static singleton).
107     * <p/>
108     * If neither the thread-local or static singleton instances are available, this method throws an
109     * {@code UnavailableSecurityManagerException} to indicate an error - a SecurityManager should always be accessible
110     * to calling code in an application. If it is not, it is likely due to a Shiro configuration problem.
111     *
112     * @return the SecurityManager accessible to the calling code.
113     * @throws UnavailableSecurityManagerException if there is no {@code SecurityManager} instance available to the
114     *                                             calling code, which typically indicates an invalid application configuration.
115     */
116    public static SecurityManager getSecurityManager() throws UnavailableSecurityManagerException {
117        SecurityManager securityManager = ThreadContext.getSecurityManager();
118        if (securityManager == null) {
119            securityManager = SecurityUtils.securityManager;
120        }
121        if (securityManager == null) {
122            String msg = "No SecurityManager accessible to the calling code, either bound to the "
123                    + ThreadContext.class.getName() + " or as a vm static singleton.  This is an invalid application "
124                    + "configuration.";
125            throw new UnavailableSecurityManagerException(msg);
126        }
127        return securityManager;
128    }
129
130    /**
131     * Returns the SecurityManager, ensuring it is of the specified type.
132     * Unwraps wrapped SecurityManagers if necessary.
133     * Caution, since this method unwraps SecurityManagers, it is possible that
134     * functionality of the wrapper is lost by the returned instance.
135     *
136     * @param type the expected type of the SecurityManager
137     * @return the SecurityManager.
138     * @param <SM> the expected type of the SecurityManager
139     */
140    public static <SM extends SecurityManager> SM getSecurityManager(Class<SM> type) {
141        Objects.requireNonNull(type, "Class argument cannot be null.");
142        return unwrapSecurityManager(getSecurityManager(), type);
143    }
144
145    /**
146     * Determines if the specified security manager is of the specified type or a subclass of the specified type.
147     *
148     * @param securityManager
149     * @param type
150     * @return true if the security manager is of the specified type or a subclass of the specified type, false otherwise.
151     */
152    public static boolean isSecurityManagerTypeOf(SecurityManager securityManager,
153                                                  Class<? extends SecurityManager> type) {
154        return type.isAssignableFrom(unwrapSecurityManager(securityManager, type).getClass());
155    }
156
157    /**
158     * Unwraps wrapped SecurityManagers if necessary.
159     * @param securityManager the SecurityManager to unwrap
160     * @param type the expected type of the SecurityManager
161     * @return the unwrapped SecurityManager
162     * @param <SM> Type of the SecurityManager
163     */
164    public static <SM extends SecurityManager> SM
165    unwrapSecurityManager(SecurityManager securityManager, Class<SM> type) {
166        return unwrapSecurityManager(securityManager, type, type::isAssignableFrom);
167    }
168
169    /**
170     * Unwraps wrapped SecurityManagers if necessary.
171     * @param securityManager the SecurityManager to unwrap
172     * @param type the expected type of the SecurityManager
173     * @param predicate to determine if the SecurityManager is of the expected type
174     * @return the unwrapped SecurityManager
175     * @param <SM> Type of the SecurityManager
176     */
177    @SuppressWarnings("unchecked")
178    public static <SM extends SecurityManager> SM
179    unwrapSecurityManager(SecurityManager securityManager, Class<SM> type,
180                          Predicate<Class<? extends SecurityManager>> predicate) {
181        while (securityManager instanceof WrappedSecurityManager && !predicate.test(securityManager.getClass())) {
182            WrappedSecurityManager wrappedSecurityManager = (WrappedSecurityManager) securityManager;
183            securityManager = wrappedSecurityManager.unwrap();
184            if (securityManager == wrappedSecurityManager) {
185                throw new IllegalStateException("SecurityManager implementation of type [" + type.getName()
186                        + "] is wrapped by itself, which is an invalid configuration.");
187            }
188        }
189        return (SM) securityManager;
190    }
191}