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");

解决方案 »

  1.   

    分享TCP/IP连接的时候必须谨慎如果一个JDBC驱动试图打开一个 TCP 连接,那么这个打开会被Java 安全管理机制自动检查。这个机构会检查当前调用栈里面有没有applet,如果有那么就限定它可以访问的机器集合。所以一般地JDBC驱动可以把TCP建立检查留给Java虚拟机。但是如果一个JDBC驱动试图在多个数据库连接之间共享一个TCP连接,那么驱动就必须自己负责检查每个调用者是否真的被允许与目标数据库联系。例如如果我们为applet A打开了一个通往机器foobah 的TCP连接,这并不意味着applet B被自动允许来共享这个连接。applet B可能没有任何访问机器foobah的权力。所以在允许某个程序重用一个现成的TCP连接之前,JDBC 驱动必须通过安全机构来检查当前的的调用者是否可以访问这个连接。通过下面的代码可是实现这个功能。SecurityManager security = System.getSecurityManager();
    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");
    }
      

  2.   

    那是不是自己还要创建一个policy文件啊? 放在那里呢? 文件的内容是什么?
      

  3.   

    是不是securityManager类必须需要一个policy文件啊!
      

  4.   

    你可以到
    http://java.sun.com/j2se/1.4/docs/guide/security/PolicyFiles.html
    看看.
      

  5.   

    关于SecurityManager更详细的资料你可以到
    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.
      

  6.   

    问题比较大,去看
    o'Reilly 的java security
    或者 inside the jvm