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.AuthenticationInfo;
023    import org.apache.shiro.authc.AuthenticationToken;
024    import org.apache.shiro.authc.Authenticator;
025    import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
026    import org.apache.shiro.util.LifecycleUtils;
027    
028    
029    /**
030     * Shiro support of a {@link SecurityManager} class hierarchy that delegates all
031     * authentication operations to a wrapped {@link Authenticator Authenticator} instance.  That is, this class
032     * implements all the <tt>Authenticator</tt> methods in the {@link SecurityManager SecurityManager}
033     * interface, but in reality, those methods are merely passthrough calls to the underlying 'real'
034     * <tt>Authenticator</tt> instance.
035     *
036     * <p>All other <tt>SecurityManager</tt> (authorization, session, etc) methods are left to be implemented by subclasses.
037     *
038     * <p>In keeping with the other classes in this hierarchy and Shiro's desire to minimize configuration whenever
039     * possible, suitable default instances for all dependencies are created upon instantiation.
040     *
041     * @author Les Hazlewood
042     * @since 0.9
043     */
044    public abstract class AuthenticatingSecurityManager extends RealmSecurityManager {
045    
046        /**
047         * The internal <code>Authenticator</code> delegate instance that this SecurityManager instance will use
048         * to perform all authentication operations.
049         */
050        private Authenticator authenticator;
051    
052        /**
053         * Default no-arg constructor that initializes its internal
054         * <code>authenticator</code> instance to a
055         * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
056         */
057        public AuthenticatingSecurityManager() {
058            super();
059            this.authenticator = new ModularRealmAuthenticator();
060        }
061    
062        /**
063         * Returns the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
064         * authentication operations.  Unless overridden by the
065         * {@link #setAuthenticator(org.apache.shiro.authc.Authenticator) setAuthenticator}, the default instance is a
066         * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
067         *
068         * @return the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
069         *         authentication operations.
070         */
071        public Authenticator getAuthenticator() {
072            return authenticator;
073        }
074    
075        /**
076         * Sets the delegate <code>Authenticator</code> instance that this SecurityManager uses to perform all
077         * authentication operations.  Unless overridden by this method, the default instance is a
078         * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}.
079         *
080         * @param authenticator the delegate <code>Authenticator</code> instance that this SecurityManager will use to
081         *                      perform all authentication operations.
082         * @throws IllegalArgumentException if the argument is <code>null</code>.
083         */
084        public void setAuthenticator(Authenticator authenticator) throws IllegalArgumentException {
085            if (authenticator == null) {
086                String msg = "Authenticator argument cannot be null.";
087                throw new IllegalArgumentException(msg);
088            }
089            this.authenticator = authenticator;
090        }
091    
092        /**
093         * Passes on the {@link #getRealms() realms} to the internal delegate <code>Authenticator</code> instance so
094         * that it may use them during authentication attempts.
095         */
096        protected void afterRealmsSet() {
097            super.afterRealmsSet();
098            if (this.authenticator instanceof ModularRealmAuthenticator) {
099                ((ModularRealmAuthenticator) this.authenticator).setRealms(getRealms());
100            }
101        }
102    
103        /**
104         * Delegates to the wrapped {@link org.apache.shiro.authc.Authenticator Authenticator} for authentication.
105         */
106        public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {
107            return this.authenticator.authenticate(token);
108        }
109    
110        public void destroy() {
111            LifecycleUtils.destroy(getAuthenticator());
112            this.authenticator = null;
113            super.destroy();
114        }
115    }