【发布时间】:2012-03-03 23:31:41
【问题描述】:
我正在阅读 Tomcat 源代码,试图弄清楚如何 Tomcat 内部受到保护,防止来自 servlet 的未经授权的访问。 我注意到的一件事是 servlet 通过 ApplicationContextFacade 获得对 StandardContext 的访问权,这似乎充当 ApplicationContext 的代理,而不是允许 servlet 直接访问 ApplicationContext。
我想知道为什么将 ApplicationContextFacade 传递给 servlet 而不是 ApplicationContext。 我怀疑这与安全性有关(因为外观几乎不是界面的简化,所以不是典型的外观模式)。 我查看了代码,发现它基本上是转发请求(如预期的那样),但是以某些安全设置(例如 Globals.IS_SECURITY_ENABLED 和 SecurityUtil.isPackageProtectionEnabled())为条件,它似乎使用 java 反射来传递请求。 我知道使用反射时权限会发生变化,但我不完全确定这将如何在 ApplicationContextFacade 中强制执行某些安全策略?
如果有人可以为我澄清这一点,那就太好了!
提前感谢您的帮助。
链接到 javadoc http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/core/ApplicationContextFacade.html
tomcat 源码链接: http://tomcat.apache.org/download-70.cgi
门面代码示例:
public String getMimeType(String file) {
if (SecurityUtil.isPackageProtectionEnabled()) {
return (String)doPrivileged("getMimeType", new Object[]{file});
} else {
return context.getMimeType(file);
}
}
其中 context 是关联的 ApplicationContext 对象 而doPrivileged定义如下:
private Object doPrivileged(final String methodName, final Object[] params){
try{
return invokeMethod(context, methodName, params);
}catch(Throwable t){
throw new RuntimeException(t.getMessage(), t);
}
}
最后调用方法
private Object invokeMethod(ApplicationContext appContext,
final String methodName,
Object[] params)
throws Throwable{
try{
Method method = (Method)objectCache.get(methodName);
if (method == null){
method = appContext.getClass()
.getMethod(methodName, (Class[])classCache.get(methodName));
objectCache.put(methodName, method);
}
return executeMethod(method,appContext,params);
} catch (Exception ex){
handleException(ex, methodName);
return null;
} finally {
params = null;
}
}
【问题讨论】:
标签: java security tomcat servlets reflection