This example enables the security manager. // Before the security manager is enabled, this call is possible
System.setProperty("java.version", "malicious data");
try {
// Enable the security manager
SecurityManager sm = new SecurityManager();
System.setSecurityManager(sm);
} catch (SecurityException se) {
// SecurityManager already set
}
// This call is no longer possible; an AccessControlException is thrown
System.setProperty("java.version", "malicious data");
System.setProperty("java.version", "malicious data");
try {
// Enable the security manager
SecurityManager sm = new SecurityManager();
System.setSecurityManager(sm);
} catch (SecurityException se) {
// SecurityManager already set
}
// This call is no longer possible; an AccessControlException is thrown
System.setProperty("java.version", "malicious data");
if (security != null)
{
security.checkConnect(hostName, portNumber);
}如果连接是不允许的,那么Security.checkConnect方法将产生一个java.lang.SecurityException。 检查所有的本地文件访问如果一个JDBC取得需要访问本地机器上的数据,那么他必须确信调用者是被允许打开这个文件的。例如:
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkRead(fileName);
}
如果对特定文件的访问是不允许的,那么Security.checkRead方法将产生一个java.lang.SecurityException。
一些驱动可能使用本地的方法来桥接底层数据库程序。则这些情况里面判断那些本地文件将被底层函数所访问是困难的。在这些环境里面用户必须作好最坏的打算,并且否决所有下载applet所发出的数据库存取,除非驱动可能完全确信将要做存取是没有问题的。
例如一个JDBC-ODBC桥接器必须检查ODBC数据源的的名称,确保applet只可以访问它的“生源地”。如果对有的名字中不能判断出数据源的主机名,那么只能否决这个访问。为了决定一个当前的调用者是可以信赖的应用还是一个applet,JDBC驱动必须能够检查这个调用者是否可以写一个随机的文件:
SecurityManager security = System.getSecurityManager();
if (security != null)
{
security.checkWrite("foobaz");
}
http://java.sun.com/j2se/1.4/docs/guide/security/PolicyFiles.html
看看.
http://java.sun.com/j2se/1.4/docs/guide/security/smPortGuide.html
去看.policy文件是需要的.
下面是关于policy的一部分资料.
By default, the JDK uses the policy files located in
file:${java.home}/lib/security/java.policy
file:${user.home}/.java.policyThese policy files are specified in the default security file:
${java.home}/lib/security/java.securityThe final policy is the union of all granted permissions in all policy files. To specify an additional policy file, you can set the java.security.policy system property at the command line:
> java -Djava.security.manager -Djava.security.policy=someURL MyApp
or
> appletviewer -J-Djava.security.policy=someURL HTMLfile
To ignore the policies in the java.security file, and only use the specified policy, use `==' instead of `=':
> java -Djava.security.manager -Djava.security.policy==someURL MyApp
Additional policy files can also be added to the java.security file.
o'Reilly 的java security
或者 inside the jvm