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     */
019    package org.apache.shiro.mgt;
020    
021    import org.apache.shiro.authc.AuthenticationException;
022    import org.apache.shiro.authc.AuthenticationToken;
023    import org.apache.shiro.authc.Authenticator;
024    import org.apache.shiro.authz.Authorizer;
025    import org.apache.shiro.session.mgt.SessionManager;
026    import org.apache.shiro.subject.Subject;
027    import org.apache.shiro.subject.SubjectContext;
028    
029    
030    /**
031     * A {@code SecurityManager} executes all security operations for <em>all</em> Subjects (aka users) across a
032     * single application.
033     * <p/>
034     * The interface itself primarily exists as a convenience - it extends the {@link org.apache.shiro.authc.Authenticator},
035     * {@link Authorizer}, and {@link SessionManager} interfaces, thereby consolidating
036     * these behaviors into a single point of reference.  For most Shiro usages, this simplifies configuration and
037     * tends to be a more convenient approach than referencing {@code Authenticator}, {@code Authorizer}, and
038     * {@code SessionManager} instances separately;  instead one only needs to interact with a single
039     * {@code SecurityManager} instance.
040     * <p/>
041     * In addition to the above three interfaces, this interface provides a number of methods supporting
042     * {@link Subject} behavior. A {@link org.apache.shiro.subject.Subject Subject} executes
043     * authentication, authorization, and session operations for a <em>single</em> user, and as such can only be
044     * managed by {@code A SecurityManager} which is aware of all three functions.  The three parent interfaces on the
045     * other hand do not 'know' about {@code Subject}s to ensure a clean separation of concerns.
046     * <p/>
047     * <b>Usage Note</b>: In actuality the large majority of application programmers won't interact with a SecurityManager
048     * very often, if at all.  <em>Most</em> application programmers only care about security operations for the currently
049     * executing user, usually attained by calling
050     * {@link org.apache.shiro.SecurityUtils#getSubject() SecurityUtils.getSubject()}.
051     * <p/>
052     * Framework developers on the other hand might find working with an actual SecurityManager useful.
053     *
054     * @author Les Hazlewood
055     * @see org.apache.shiro.mgt.DefaultSecurityManager
056     * @since 0.2
057     */
058    public interface SecurityManager extends Authenticator, Authorizer, SessionManager {
059    
060        /**
061         * Logs in the specified Subject using the given {@code authenticationToken}, returning an updated Subject
062         * instance reflecting the authenticated state if successful or throwing {@code AuthenticationException} if it is
063         * not.
064         * <p/>
065         * Note that most application developers should probably not call this method directly unless they have a good
066         * reason for doing so.  The preferred way to log in a Subject is to call
067         * <code>subject.{@link org.apache.shiro.subject.Subject#login login(authenticationToken)}</code> (usually after
068         * acquiring the Subject by calling {@link org.apache.shiro.SecurityUtils#getSubject() SecurityUtils.getSubject()}).
069         * <p/>
070         * Framework developers on the other hand might find calling this method directly useful in certain cases.
071         *
072         * @param subject             the subject against which the authentication attempt will occur
073         * @param authenticationToken the token representing the Subject's principal(s) and credential(s)
074         * @return the subject instance reflecting the authenticated state after a successful attempt
075         * @throws AuthenticationException if the login attempt failed.
076         * @since 1.0
077         */
078        Subject login(Subject subject, AuthenticationToken authenticationToken) throws AuthenticationException;
079    
080        /**
081         * Logs out the specified Subject from the system.
082         * <p/>
083         * Note that most application developers should not call this method unless they have a good reason for doing
084         * so.  The preferred way to logout a Subject is to call
085         * <code>{@link org.apache.shiro.subject.Subject#logout Subject.logout()}</code>, not the
086         * {@code SecurityManager} directly.
087         * <p/>
088         * Framework developers on the other hand might find calling this method directly useful in certain cases.
089         *
090         * @param subject the subject to log out.
091         * @since 1.0
092         */
093        void logout(Subject subject);
094    
095        /**
096         * Creates a {@code Subject} instance reflecting the specified contextual data.
097         * <p/>
098         * The context can be anything needed by this {@code SecurityManager} to construct a {@code Subject} instance.
099         * Most Shiro end-users will never call this method - it exists primarily for
100         * framework development and to support any underlying custom {@link SubjectFactory SubjectFactory} implementations
101         * that may be used by the {@code SecurityManager}.
102         * <h4>Usage</h4>
103         * After calling this method, the returned instance is <em>not</em> bound to the application for further use.
104         * Callers are expected to know that {@code Subject} instances have local scope only and any
105         * other further use beyond the calling method must be managed explicitly.
106         *
107         * @param context any data needed to direct how the Subject should be constructed.
108         * @return the {@code Subject} instance reflecting the specified initialization data.
109         * @see SubjectFactory#createSubject(SubjectContext)
110         * @see Subject.Builder
111         * @since 1.0
112         */
113        Subject createSubject(SubjectContext context);
114    
115    }