/* * 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.catalina.security;
/** * This utility class associates a <code>Subject</code> to the current <code>AccessControlContext</code>. When a * <code>SecurityManager</code> is used, the container will always associate the called thread with an * AccessControlContext containing only the principal of the requested Servlet/Filter. This class uses reflection to * invoke the methods.
*/
/** * The string resources for this package.
*/ privatestaticfinal StringManager sm = StringManager.getManager(Constants.PACKAGE);
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will be called. * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Servlet targetObject) throws Exception {
doAsPrivilege(methodName, targetObject, null, null, null);
}
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will be called. * @param targetType <code>Class</code> array used to instantiate a <code>Method</code> object. * @param targetArguments <code>Object</code> array contains the runtime parameters instance. * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Servlet targetObject, finalClass<?>[] targetType, final Object[] targetArguments) throws Exception {
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will be called. * @param targetParameterTypes <code>Class</code> array used to instantiate a <code>Method</code> object. * @param targetArguments <code>Object</code> array contains the runtime parameters instance. * @param principal the <code>Principal</code> to which the security privilege applies * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Servlet targetObject, finalClass<?>[] targetParameterTypes, final Object[] targetArguments, Principal principal) throws Exception {
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Filter</code> on which the method will be called. * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Filter targetObject) throws Exception {
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Filter</code> on which the method will be called. * @param targetType <code>Class</code> array used to instantiate a <code>Method</code> object. * @param targetArguments <code>Object</code> array contains the runtime parameters instance. * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Filter targetObject, finalClass<?>[] targetType, final Object[] targetArguments) throws Exception {
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param methodName the method to apply the security restriction * @param targetObject the <code>Filter</code> on which the method will be called. * @param targetParameterTypes <code>Class</code> array used to instantiate a <code>Method</code> object. * @param targetParameterValues <code>Object</code> array contains the runtime parameters instance. * @param principal the <code>Principal</code> to which the security privilege applies * * @throws Exception an execution error occurred
*/ publicstaticvoid doAsPrivilege(final String methodName, final Filter targetObject, finalClass<?>[] targetParameterTypes, final Object[] targetParameterValues, Principal principal) throws Exception {
/** * Perform work as a particular <code>Subject</code>. Here the work will be granted to a <code>null</code> subject. * * @param method the method to apply the security restriction * @param targetObject the <code>Servlet</code> on which the method will be called. * @param targetArguments <code>Object</code> array contains the runtime parameters instance. * @param principal the <code>Principal</code> to which the security privilege applies * * @throws Exception an execution error occurred
*/ privatestaticvoid execute(final Method method, final Object targetObject, final Object[] targetArguments,
Principal principal) throws Exception {
// The first argument is always the request object if (targetArguments != null && targetArguments[0] instanceof HttpServletRequest) {
HttpServletRequest request = (HttpServletRequest) targetArguments[0];
Subject.doAsPrivileged(subject, pea, null);
} catch (PrivilegedActionException pe) {
Throwable e; if (pe.getException() instanceof InvocationTargetException) {
e = pe.getException().getCause();
ExceptionUtils.handleThrowable(e);
} else {
e = pe;
}
if (log.isDebugEnabled()) {
log.debug(sm.getString("SecurityUtil.doAsPrivilege"), e);
}
if (e instanceof UnavailableException) { throw (UnavailableException) e;
} elseif (e instanceof ServletException) { throw (ServletException) e;
} elseif (e instanceof IOException) { throw (IOException) e;
} elseif (e instanceof RuntimeException) { throw (RuntimeException) e;
} else { thrownew ServletException(e.getMessage(), e);
}
}
}
/** * Find a method stored within the cache. * * @param methodsCache the cache used to store method instance * @param methodName the method to apply the security restriction * * @return the method instance, null if not yet created.
*/ privatestatic Method findMethod(Method[] methodsCache, String methodName) { if (methodName.equals(INIT_METHOD)) { return methodsCache[INIT];
} elseif (methodName.equals(DESTROY_METHOD)) { return methodsCache[DESTROY];
} elseif (methodName.equals(SERVICE_METHOD)) { return methodsCache[SERVICE];
} elseif (methodName.equals(DOFILTER_METHOD)) { return methodsCache[DOFILTER];
} elseif (methodName.equals(EVENT_METHOD)) { return methodsCache[EVENT];
} elseif (methodName.equals(DOFILTEREVENT_METHOD)) { return methodsCache[DOFILTEREVENT];
} returnnull;
}
/** * Create the method and cache it for further re-use. * * @param methodsCache the cache used to store method instance * @param targetType the class on which the method will be called. * @param methodName the method to apply the security restriction * @param parameterTypes <code>Class</code> array used to instantiate a <code>Method</code> object. * * @return the method instance. * * @throws Exception an execution error occurred
*/ privatestatic Method createMethodAndCacheIt(Method[] methodsCache, Class<?> targetType, String methodName, Class<?>[] parameterTypes) throws Exception {
if (methodsCache == null) {
methodsCache = new Method[4];
}
/** * Remove the object from the cache. * * @param cachedObject The object to remove
*/ publicstaticvoid remove(Object cachedObject) {
classCache.remove(cachedObject);
}
/** * Return the <code>SecurityManager</code> only if Security is enabled AND package protection mechanism is enabled. * * @return <code>true</code> if package level protection is enabled
*/ publicstaticboolean isPackageProtectionEnabled() { if (packageDefinitionEnabled && Globals.IS_SECURITY_ENABLED) { returntrue;
} returnfalse;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.