链接 用户手册 参考 Apache Tomcat 开发 | 许可 Permissions |
Java的Permission类是用来定义Tomcat载入的类所拥有的权限。Java本身包括了一些
Permission类,你也可以在你的网络应用中加入你自己的Permission类。
这两种技术在Tomcat 5里都被应用。
标准许可 |
这里将简单总结标准系统中适用于Tomcat的SecurityManager 和 Permission 类。
更多信息请参看
http://java.sun.com/security/。
- java.util.PropertyPermission
- 控制读/写Java虚拟器的属性,如
java.home 。
- java.lang.RuntimePermission
- 控制使用一些系统/运行时(System/Runtime)的功能,如
exit()
和 exec() 。它也控制包(package)的访问/定义。
- java.io.FilePermission
- 控制对文件和目录的读/写/执行操作。
- java.net.SocketPermission
- 控制使用网路sockets连接。
- java.net.NetPermission
- 控制使用multicast网路连接。
- java.lang.reflect.ReflectPermission
- 控制使用reflection来对类进行检视。
- java.security.SecurityPermission
- 控制对安全方法的访问。
- java.security.AllPermission
- 给予所有访问权限,就如你运行一个没有SecurityManager的Tomcat 。
|
|
改变 Tomcat 中的 SecurityManager |
政策文件的格式
由Java SecurityManager实现的安全政策被配置存放在
$CATALINA_HOME/conf/catalina.policy 文件里。这个文件完全替代了
JDK系统目录里的java.policy 文件。这个catalina.policy 文件可以手动修改,
或者使用Java 1.2 及其后版本的 policytool 程序修改。
catalina.policy 文件中的条文使用了标准的java.policy
文件格式,如下:
| | | | // Example policy file entry
grant [signedBy <signer>,] [codeBase <code source>] {
permission <class> [<name> [, <action list>]];
}; | | | | |
其中signedBy 和 codeBase 是选择项。
注释行是以"//"开始,直到该行结束。codeBase是URL的格式,文件的URL中可用如
${java.home}和${catalina.home}等属性(这些属性会被扩展到由环境变量
JAVA_HOME 和 CATALINA_HOME 为他们定义的目录路径)。
缺省政策文件
缺省$CATALINA_HOME/conf/catalina.policy 文件看起来象这样:
| | | |
// ============================================================================
// catalina.corepolicy - Security Policy Permissions for Tomcat 5
//
// This file contains a default set of security policies to be enforced (by the
// JVM) when Catalina is executed with the "-security" option. In addition
// to the permissions granted here, the following additional permissions are
// granted to the codebase specific to each web application:
//
// * Read access to the document root directory
//
// $Id: security-manager-howto.xml 301460 2003-01-15 03:40:45Z glenn $
// ============================================================================
// ========== SYSTEM CODE PERMISSIONS =========================================
// ========== 系统源码许可控制 ===============
// These permissions apply to javac
// 下面的许可适用于javac
grant codeBase "file:${java.home}/lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions
// 下面的许可适用于java的扩展
grant codeBase "file:${java.home}/jre/lib/ext/-" {
permission java.security.AllPermission;
};
// These permissions apply to javac when ${java.home] points at $JAVA_HOME/jre
// 下面的许可适用于当${java.home] 是 $JAVA_HOME/jre时的 javac
grant codeBase "file:${java.home}/../lib/-" {
permission java.security.AllPermission;
};
// These permissions apply to all shared system extensions when
// 下面的许可适用于java的扩展
// ${java.home} points at $JAVA_HOME/jre
grant codeBase "file:${java.home}/lib/ext/-" {
permission java.security.AllPermission;
};
// ========== CATALINA CODE PERMISSIONS =======================================
// ========== CATALINA的源码许可控制 =============
// These permissions apply to the launcher code
// 以下许可适用于启动Tomcat的程序
grant codeBase "file:${catalina.home}/bin/commons-launcher.jar" {
permission java.security.AllPermission;
};
// These permissions apply to the server startup code
// 以下许可适用于启动服务器的程序
grant codeBase "file:${catalina.home}/bin/bootstrap.jar" {
permission java.security.AllPermission;
};
// These permissions apply to the servlet API classes
// and those that are shared across all class loaders
// located in the "common" directory
// 以下许可适用于 Servlet 的 API 和 common目录里的程序
grant codeBase "file:${catalina.home}/common/-" {
permission java.security.AllPermission;
};
// These permissions apply to the container's core code, plus any additional
// libraries installed in the "server" directory
// 下面的许可适用于Tomcat的核心和 server 目录里的程序
grant codeBase "file:${catalina.home}/server/-" {
permission java.security.AllPermission;
};
// ========== WEB APPLICATION PERMISSIONS =====================================
// ========== WEB 应用的许可 ===========
// These permissions are granted by default to all web applications
// In addition, a web application will be given a read FilePermission
// and JndiPermission for all files and directories in its document root.
// 缺省情况下,所有WEB 应用拥有下面这些许可,另外还有 它们本身文件夹里的 “read” FilePermission
// 和JndiPermission。
grant {
// Required for JNDI lookup of named JDBC DataSource's and
// javamail named MimePart DataSource used to send mail
permission java.util.PropertyPermission "java.home", "read";
permission java.util.PropertyPermission "java.naming.*", "read";
permission java.util.PropertyPermission "javax.sql.*", "read";
// OS Specific properties to allow read access
permission java.util.PropertyPermission "os.name", "read";
permission java.util.PropertyPermission "os.version", "read";
permission java.util.PropertyPermission "os.arch", "read";
permission java.util.PropertyPermission "file.separator", "read";
permission java.util.PropertyPermission "path.separator", "read";
permission java.util.PropertyPermission "line.separator", "read";
// JVM properties to allow read access
permission java.util.PropertyPermission "java.version", "read";
permission java.util.PropertyPermission "java.vendor", "read";
permission java.util.PropertyPermission "java.vendor.url", "read";
permission java.util.PropertyPermission "java.class.version", "read";
permission java.util.PropertyPermission "java.specification.version", "read";
permission java.util.PropertyPermission "java.specification.vendor", "read";
permission java.util.PropertyPermission "java.specification.name", "read";
permission java.util.PropertyPermission "java.vm.specification.version", "read";
permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
permission java.util.PropertyPermission "java.vm.specification.name", "read";
permission java.util.PropertyPermission "java.vm.version", "read";
permission java.util.PropertyPermission "java.vm.vendor", "read";
permission java.util.PropertyPermission "java.vm.name", "read";
// Required for getting BeanInfo
permission java.lang.RuntimePermission "accessClassInPackage.sun.beans.*";
// Required for OpenJMX
permission java.lang.RuntimePermission "getAttribute";
// Allow read of JAXP compliant XML parser debug
permission java.util.PropertyPermission "jaxp.debug", "read";
};
// 你还可以给某个特定的WEB应用赋予其它许可。
// You can assign additional permissions to particular web applications by
// adding additional "grant" entries here, based on the code base for that
// application, /WEB-INF/classes/, or /WEB-INF/lib/ jar files.
//
// Different permissions can be granted to JSP pages, classes loaded from
// the /WEB-INF/classes/ directory, all jar files in the /WEB-INF/lib/
// directory, or even to individual jar files in the /WEB-INF/lib/ directory.
//
// For instance, assume that the standard "examples" application
// included a JDBC driver that needed to establish a network connection to the
// corresponding database and used the scrape taglib to get the weather from
// the NOAA web server. You might create a "grant" entries like this:
//
// The permissions granted to the context root directory apply to JSP pages.
// grant codeBase "file:${catalina.home}/webapps/examples/-" {
// permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
// permission java.net.SocketPermission "*.noaa.gov:80", "connect";
// };
//
// The permissions granted to the context WEB-INF/classes directory
// grant codeBase "file:${catalina.home}/webapps/examples/WEB-INF/classes/-" {
// };
//
// The permission granted to your JDBC driver
// grant codeBase "jar:file:${catalina.home}/webapps/examples/WEB-INF/lib/driver.jar!/-" {
// permission java.net.SocketPermission "dbhost.mycompany.com:5432", "connect";
// };
// The permission granted to the scrape taglib
// grant codeBase "jar:file:${catalina.home}/webapps/examples/WEB-INF/lib/scrape.jar!/-" {
// permission java.net.SocketPermission "*.noaa.gov:80", "connect";
// };
| | | | |
启动附带SecurityManager的Tomcat
在你配置好与SecurityManager一起使用的catalina.policy 文件之后,
你可以使用"-security"选项来启动Tomcat。
| | | | $CATALINA_HOME/bin/catalina.sh start -security (Unix)
%CATALINA_HOME%\bin\catalina start -security (Windows) | | | | |
|
Configuring Package Protection in Tomcat |
从Tomcat 5开始,现在可以配置Tomcat内部包的许可。更多信息请参阅
http://java.sun.com/security/seccodeguide.html
。
警告:删除缺省的包保护,可能打开一个安全漏洞。
缺省的属性文件
缺省的$CATALINA_HOME/conf/catalina.properties 文件看起来象这样:
| | | | #
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageAccess unless the
# corresponding RuntimePermission ("accessClassInPackage."+package) has
# been granted.
package.access=sun.,org.apache.catalina.,org.apache.coyote.,org.apache.tomcat.,
org.apache.jasper.
#
# List of comma-separated packages that start with or equal this string
# will cause a security exception to be thrown when
# passed to checkPackageDefinition unless the
# corresponding RuntimePermission ("defineClassInPackage."+package) has
# been granted.
#
# by default, no packages are restricted for definition, and none of
# the class loaders supplied with the JDK call checkPackageDefinition.
#
package.definition=sun.,java.,org.apache.catalina.,org.apache.coyote.,
org.apache.tomcat.,org.apache.jasper. | | | | |
当你完成配置SecurityManager所需的catalina.properties 文件,记住要重新启动Tomcat。
|
|