将 was.policy 文件添加到 Java 2 安全性应用程序
如果应用程序访问需要比缺省 app.policy 文件中授予的许可权多的许可权的资源,那么它可能需要 was.policy 文件。
关于此任务
所有应用程序的缺省策略文件是在 app.policy 文件中指定的。此文件由产品安全性提供,它是所有应用程序的公共文件,请不要更改此文件。在 was.policy 文件中添加应用程序必需的任何新的许可权。
WebSphere Application Server 提供的 app.policy 文件位于 app_server_root/config/cells/profile/profile_name/config/cell_name/nodes/node_name/app.policy 目录中。 在以下示例中提供了 app.policy 文件的内容:
// The following permissions apply to all the components under the application.
grant codeBase "file:${application}" {
// The following are required by JavaMail
permission java.io.FilePermission "
${was.install.root}${/}lib${/}activation-impl.jar",
"read";
permission java.io.FilePermission "
${was.install.root}${/}lib${/}mail-impl.jar","read";
};
// The following permissions apply to all utility .jar files (other
// than enterprise beans JAR files) in the application.
grant codeBase "file:${jars}" {
permission java.net.SocketPermission "*", "connect";
permission java.util.PropertyPermission "*", "read";
};
// The following permissions apply to connector resources within the application
grant codeBase "file:${connectorComponent}" {
permission java.net.SocketPermission "*", "connect";
permission java.util.PropertyPermission "*", "read";
};
// The following permissions apply to all the web modules (.war files)
// within the application.
grant codeBase "file:${webComponent}" {
permission java.io.FilePermission "${was.module.path}${/}-", "read, write";
// where "was.module.path" is the path where the web module is
// installed. Refer to Dynamic policy concepts for other symbols.
permission java.lang.RuntimePermission "loadLibrary.*";
permission java.lang.RuntimePermission "queuePrintJob";
permission java.net.SocketPermission "*", "connect";
permission java.util.PropertyPermission "*", "read";
};
// The following permissions apply to all the EJB modules within the application.
grant codeBase "file:${ejbComponent}" {
permission java.lang.RuntimePermission "queuePrintJob";
permission java.net.SocketPermission "*", "connect";
permission java.util.PropertyPermission "*", "read";
};
如果对于应用程序或应用程序的一个或多个模块,附加许可权是必需的,那么将 was.policy 文件用于此应用程序。例如,使用 ${application} 的 codeBase 并添加必需的许可权以将附加许可权授予整个应用程序。 同样,使用 ${webComponent} 和 ${ejbComponent} 的 codeBase 将附加许可权授予应用程序中的所有 Web 模块和所有企业 Bean 模块。您可将附加许可权指定给每个模块(.war 文件或 .jar 文件),如以下示例中所示。
本示例举例说明如何在 was.policy 文件中为应用程序添加额外许可权:
// grant additional permissions to a web module
grant codeBase " file:aWebModule.war" {
permission java.security.SecurityPermission "printIdentity";
};
// grant additional permission to an EJB module
grant codeBase "file:aEJBModule.jar" {
permission java.io.FilePermission "
${user.install.root}${/}bin${/}DefaultDB${/}-", "read,write,delete";
// where, ${user.install.root} is the system property whose value is
// located in the app_server_root directory.
};
要将 was.policy 文件用于应用程序,请执行以下步骤:
过程
结果
示例
此步骤是启用 Java 2 安全性时正确运行应用程序必需的。如果未创建 was.policy 文件,并且它不包含必需的许可权,那么应用程序可能无法访问系统资源。
缺少许可权的症状是 java.security.AccessControlException 异常。缺少的许可权列出在异常数据中,例如,
![[IBM i]](../images/iseries.gif)
![[z/OS]](../images/ngzos.gif)
java.security.AccessControlException: access denied (java.io.FilePermission
${was.install.root}/java/ext/mail.jar read)
![[IBM i]](../images/iseries.gif)
![[z/OS]](../images/ngzos.gif)
grant codeBase "file:user_client_installed_location" {
permission java.io.FilePermission
"${was.install.root}$(/)java$(/)jre$(/)lib$(/)ext$(/)mail.jar", "read";
};
为了便于说明,上几行许可权信息被分割。请在一行上输入许可权。