All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.shiro.mgt.AuthenticatingSecurityManager Maven / Gradle / Ivy

There is a newer version: 3.9
Show newest version
/*
 * 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.mgt;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.Authenticator;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.util.LifecycleUtils;


/**
 * Shiro support of a {@link SecurityManager} class hierarchy that delegates all
 * authentication operations to a wrapped {@link Authenticator Authenticator} instance.  That is, this class
 * implements all the Authenticator methods in the {@link SecurityManager SecurityManager}
 * interface, but in reality, those methods are merely passthrough calls to the underlying 'real'
 * Authenticator instance.
 *
 * 

All other SecurityManager (authorization, session, etc) methods are left to be implemented by subclasses. * *

In keeping with the other classes in this hierarchy and Shiro's desire to minimize configuration whenever * possible, suitable default instances for all dependencies are created upon instantiation. * * @since 0.9 */ public abstract class AuthenticatingSecurityManager extends RealmSecurityManager { /** * The internal Authenticator delegate instance that this SecurityManager instance will use * to perform all authentication operations. */ private Authenticator authenticator; /** * Default no-arg constructor that initializes its internal * authenticator instance to a * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}. */ public AuthenticatingSecurityManager() { super(); this.authenticator = new ModularRealmAuthenticator(); } /** * Returns the delegate Authenticator instance that this SecurityManager uses to perform all * authentication operations. Unless overridden by the * {@link #setAuthenticator(org.apache.shiro.authc.Authenticator) setAuthenticator}, the default instance is a * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}. * * @return the delegate Authenticator instance that this SecurityManager uses to perform all * authentication operations. */ public Authenticator getAuthenticator() { return authenticator; } /** * Sets the delegate Authenticator instance that this SecurityManager uses to perform all * authentication operations. Unless overridden by this method, the default instance is a * {@link org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator}. * * @param authenticator the delegate Authenticator instance that this SecurityManager will use to * perform all authentication operations. * @throws IllegalArgumentException if the argument is null. */ public void setAuthenticator(Authenticator authenticator) throws IllegalArgumentException { if (authenticator == null) { String msg = "Authenticator argument cannot be null."; throw new IllegalArgumentException(msg); } this.authenticator = authenticator; } /** * Passes on the {@link #getRealms() realms} to the internal delegate Authenticator instance so * that it may use them during authentication attempts. */ protected void afterRealmsSet() { super.afterRealmsSet(); if (this.authenticator instanceof ModularRealmAuthenticator) { ((ModularRealmAuthenticator) this.authenticator).setRealms(getRealms()); } } /** * Delegates to the wrapped {@link org.apache.shiro.authc.Authenticator Authenticator} for authentication. */ public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException { return this.authenticator.authenticate(token); } public void destroy() { LifecycleUtils.destroy(getAuthenticator()); this.authenticator = null; super.destroy(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy