📄 abstractsecurityinterceptor.java
字号:
/* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited * * Licensed 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.acegisecurity.intercept;import org.acegisecurity.AccessDecisionManager;import org.acegisecurity.AccessDeniedException;import org.acegisecurity.AcegiMessageSource;import org.acegisecurity.AfterInvocationManager;import org.acegisecurity.Authentication;import org.acegisecurity.AuthenticationCredentialsNotFoundException;import org.acegisecurity.AuthenticationException;import org.acegisecurity.AuthenticationManager;import org.acegisecurity.ConfigAttribute;import org.acegisecurity.ConfigAttributeDefinition;import org.acegisecurity.RunAsManager;import org.acegisecurity.context.SecurityContextHolder;import org.acegisecurity.event.authorization.AuthenticationCredentialsNotFoundEvent;import org.acegisecurity.event.authorization.AuthorizationFailureEvent;import org.acegisecurity.event.authorization.AuthorizedEvent;import org.acegisecurity.event.authorization.PublicInvocationEvent;import org.acegisecurity.runas.NullRunAsManager;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.InitializingBean;import org.springframework.context.ApplicationEvent;import org.springframework.context.ApplicationEventPublisher;import org.springframework.context.ApplicationEventPublisherAware;import org.springframework.context.MessageSource;import org.springframework.context.MessageSourceAware;import org.springframework.context.support.MessageSourceAccessor;import org.springframework.util.Assert;import java.util.HashSet;import java.util.Iterator;import java.util.Set;/** * Abstract class that implements security interception for secure objects. * <p> * The <code>AbstractSecurityInterceptor</code> will ensure the proper startup * configuration of the security interceptor. It will also implement the proper * handling of secure object invocations, being: * <ol> * <li>Obtain the {@link Authentication} object from the * {@link SecurityContextHolder}.</li> * <li>Determine if the request relates to a secured or public invocation by * looking up the secure object request against the * {@link ObjectDefinitionSource}.</li> * <li>For an invocation that is secured (there is a * <code>ConfigAttributeDefinition</code> for the secure object invocation): * <ol type="a"> * <li>If either the {@link org.acegisecurity.Authentication#isAuthenticated()} * returns <code>false</code>, or the {@link #alwaysReauthenticate} is * <code>true</code>, authenticate the request against the configured * {@link AuthenticationManager}. When authenticated, replace the * <code>Authentication</code> object on the * <code>SecurityContextHolder</code> with the returned value.</li> * <li>Authorize the request against the configured * {@link AccessDecisionManager}.</li> * <li>Perform any run-as replacement via the configured {@link RunAsManager}.</li> * <li>Pass control back to the concrete subclass, which will actually proceed * with executing the object. A {@link InterceptorStatusToken} is returned so * that after the subclass has finished proceeding with execution of the object, * its finally clause can ensure the <code>AbstractSecurityInterceptor</code> * is re-called and tidies up correctly.</li> * <li>The concrete subclass will re-call the * <code>AbstractSecurityInterceptor</code> via the * {@link #afterInvocation(InterceptorStatusToken, Object)} method.</li> * <li>If the <code>RunAsManager</code> replaced the * <code>Authentication</code> object, return the * <code>SecurityContextHolder</code> to the object that existed after the * call to <code>AuthenticationManager</code>.</li> * <li>If an <code>AfterInvocationManager</code> is defined, invoke the * invocation manager and allow it to replace the object due to be returned to * the caller.</li> * </ol> * </li> * <li>For an invocation that is public (there is no * <code>ConfigAttributeDefinition</code> for the secure object invocation): * <ol type="a"> * <li>As described above, the concrete subclass will be returned an * <code>InterceptorStatusToken</code> which is subsequently re-presented to * the <code>AbstractSecurityInterceptor</code> after the secure object has * been executed. The <code>AbstractSecurityInterceptor</code> will take no * further action when its {@link #afterInvocation(InterceptorStatusToken, * Object)} is called.</li> * </ol> * </li> * <li>Control again returns to the concrete subclass, along with the * <code>Object</code> that should be returned to the caller. The subclass * will then return that result or exception to the original caller.</li> * </ol> * </p> * * @author Ben Alex * @version $Id: AbstractSecurityInterceptor.java 1790 2007-03-30 18:27:19Z * luke_t $ */public abstract class AbstractSecurityInterceptor implements InitializingBean, ApplicationEventPublisherAware, MessageSourceAware { // ~ Static fields/initializers // ===================================================================================== protected static final Log logger = LogFactory.getLog(AbstractSecurityInterceptor.class); // ~ Instance fields // ================================================================================================ private AccessDecisionManager accessDecisionManager; private AfterInvocationManager afterInvocationManager; private ApplicationEventPublisher eventPublisher; private AuthenticationManager authenticationManager; protected MessageSourceAccessor messages = AcegiMessageSource.getAccessor(); private RunAsManager runAsManager = new NullRunAsManager(); private boolean alwaysReauthenticate = false; private boolean rejectPublicInvocations = false; private boolean validateConfigAttributes = true; // ~ Methods // ======================================================================================================== /** * Completes the work of the <code>AbstractSecurityInterceptor</code> * after the secure object invocation has been complete * * @param token as returned by the {@link #beforeInvocation(Object)}} * method * @param returnedObject any object returned from the secure object * invocation (may be<code>null</code>) * * @return the object the secure object invocation should ultimately return * to its caller (may be <code>null</code>) */ protected Object afterInvocation(InterceptorStatusToken token, Object returnedObject) { if (token == null) { // public object return returnedObject; } if (token.isContextHolderRefreshRequired()) { if (logger.isDebugEnabled()) { logger.debug("Reverting to original Authentication: " + token.getAuthentication().toString()); } SecurityContextHolder.getContext().setAuthentication(token.getAuthentication()); } if (afterInvocationManager != null) { // Attempt after invocation handling try { returnedObject = afterInvocationManager.decide(token.getAuthentication(), token.getSecureObject(), token.getAttr(), returnedObject); } catch (AccessDeniedException accessDeniedException) { AuthorizationFailureEvent event = new AuthorizationFailureEvent(token.getSecureObject(), token .getAttr(), token.getAuthentication(), accessDeniedException); publishEvent(event); throw accessDeniedException; } } return returnedObject; } public void afterPropertiesSet() throws Exception { Assert.notNull(getSecureObjectClass(), "Subclass must provide a non-null response to getSecureObjectClass()"); Assert.notNull(this.messages, "A message source must be set"); Assert.notNull(this.authenticationManager, "An AuthenticationManager is required"); Assert.notNull(this.accessDecisionManager, "An AccessDecisionManager is required"); Assert.notNull(this.runAsManager, "A RunAsManager is required"); Assert.notNull(this.obtainObjectDefinitionSource(), "An ObjectDefinitionSource is required"); Assert.isTrue(this.obtainObjectDefinitionSource().supports(getSecureObjectClass()), "ObjectDefinitionSource does not support secure object class: " + getSecureObjectClass()); Assert.isTrue(this.runAsManager.supports(getSecureObjectClass()), "RunAsManager does not support secure object class: " + getSecureObjectClass()); Assert.isTrue(this.accessDecisionManager.supports(getSecureObjectClass()), "AccessDecisionManager does not support secure object class: " + getSecureObjectClass()); if (this.afterInvocationManager != null) { Assert.isTrue(this.afterInvocationManager.supports(getSecureObjectClass()), "AfterInvocationManager does not support secure object class: " + getSecureObjectClass()); } if (this.validateConfigAttributes) { Iterator iter = this.obtainObjectDefinitionSource().getConfigAttributeDefinitions(); if (iter == null) { logger.warn("Could not validate configuration attributes as the MethodDefinitionSource did not return " + "a ConfigAttributeDefinition Iterator"); return; } Set unsupportedAttrs = new HashSet(); while (iter.hasNext()) { ConfigAttributeDefinition def = (ConfigAttributeDefinition) iter.next(); Iterator attributes = def.getConfigAttributes(); while (attributes.hasNext()) { ConfigAttribute attr = (ConfigAttribute) attributes.next(); if (!this.runAsManager.supports(attr) && !this.accessDecisionManager.supports(attr) && ((this.afterInvocationManager == null) || !this.afterInvocationManager.supports(attr))) { unsupportedAttrs.add(attr); } } } if (unsupportedAttrs.size() != 0) { throw new IllegalArgumentException("Unsupported configuration attributes: " + unsupportedAttrs); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -