java.policy文件位于%JAVA_HOME%/ jre/lib/security/下,默认内容如下:
// Standard extensions get all permissions by default grant codeBase "file<img src="%5C%22static/image/smiley/default/shy.gif%5C%22" smilieid="\"8\"" alt="\"\"" border="\"0\"">{{java.ext.dirs}}/*" { permission java.security.AllPermission; }; // default permissions granted to all domains grant { // Allows any thread to stop itself using the java.lang.Thread.stop() // method that takes no argument. // Note that this permission is granted by default only to remain // backwards compatible. // It is strongly recommended that you either remove this permission // from this policy file or further restrict it to code sources // that you specify, because Thread.stop() is potentially unsafe. // See "http://java.sun.com/notes" for more information. permission java.lang.RuntimePermission "stopThread"; // allows anyone to listen on un-privileged ports permission java.net.SocketPermission "localhost:1024-", "listen"; // "standard" properies that can be read by anyone 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 "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"; 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"; };
3.先写个测试例子:JavaPolicyTest.java(将其放在C:/TEMP目录下)
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStream; import java.io.InputStreamReader; public class JavaPolicyTest { public static void main(String[] args) { File file = new File("D:/test.txt"); try { read(file); System.out.println("file read ok"); } catch (Throwable e) { System.out.println(e.getMessage()); } try { write(file); System.out.println("file write ok"); } catch (Throwable e) { System.out.println(e.getMessage()); } } private static void read(File file) throws Throwable { InputStream in = null; BufferedReader reader = null; try { in = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(in)); String temp = null; while ((temp = reader.readLine()) != null) { System.out.println("read-->" + temp); } } catch (Throwable e) { throw e; } finally { if (in != null) { in.close(); } if (reader != null) { reader.close(); } } } private static void write(File file) throws Throwable { FileWriter fw = new FileWriter(file); for (int i = 0; i < 10; i++) { String temp = new java.util.Date() + " " + new java.util.Random().nextLong(); System.out.println("write-->" + temp); fw.write(temp + "\r\n"); } fw.flush(); fw.close(); } }
grant codeBase "file:/C:/TMP/*" { permission java.security.AllPermission; };