JAVA文件访问控制

来源:互联网 发布:炒股模拟软件 知乎 编辑:程序博客网 时间:2024/06/05 17:13

JAVA访问控制具备四种权限read、write、delete、execute,implies方法用来确定某个线程是否拥有这些操作的权限

    public boolean implies(Permission p) {
if (!(p instanceof FilePermission))
   return false;
FilePermission that = (FilePermission) p;
return ((this.mask & that.mask) == that.mask) && impliesIgnoreMask(that);
    }

    boolean impliesIgnoreMask(FilePermission that) {
if (this.directory) {
   if (this.recursive) {
// make sure that.path is longer then path so
// something like /foo/- does not imply /foo
if (that.directory) {
   return (that.cpath.length() >= this.cpath.length()) &&
   that.cpath.startsWith(this.cpath);
}  else {
   return ((that.cpath.length() > this.cpath.length()) &&
       that.cpath.startsWith(this.cpath));
}
   } else {
if (that.directory) {
   // if the permission passed in is a directory
   // specification, make sure that a non-recursive
   // permission (i.e., this object) can't imply a recursive
   // permission.
   if (that.recursive)
return false;
   else 
return (this.cpath.equals(that.cpath));
} else {
   int last = that.cpath.lastIndexOf(File.separatorChar);
   if (last == -1) 
return false;
   else {
// this.cpath.equals(that.cpath.substring(0, last+1));
// Use regionMatches to avoid creating new string


return (this.cpath.length() == (last + 1)) &&
   this.cpath.regionMatches(0, that.cpath, 0, last+1);
   }
}
   }
} else {
   return (this.cpath.equals(that.cpath));
}
    }

例子如下

package com.test;


import java.io.File;
import java.io.FilePermission;
import java.security.Permission;


public class Test2{

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, CloneNotSupportedException {
char s = File.separatorChar;
Permission file = new FilePermission("C:"+s+"Windows"+s+"f", "read");
Permission file1 = new FilePermission("C:"+s+"Windows"+s+"*", "read");
Permission file2 = new FilePermission("C:"+s+"Windows"+s+"f", "write");
Permission file3 = new FilePermission("C:"+s+"Windows"+s+"*", "write");
Permission file4 = new FilePermission("C:"+s+"Windows"+s+"f", "delete");
Permission file5 = new FilePermission("C:"+s+"Windows"+s+"*", "delete");
Permission file6 = new FilePermission("C:"+s+"Windows"+s+"f", "execute");
Permission file7 = new FilePermission("C:"+s+"Windows"+s+"*", "execute");


boolean f = file.implies(file);
boolean f1 = file.implies(file1);
boolean f2 = file.implies(file2);
boolean f3 = file.implies(file3);
boolean f4 = file.implies(file4);
boolean f5 = file.implies(file5);
boolean f6 = file.implies(file6);
boolean f7 = file.implies(file7);
System.out.println("file:"+f);
System.out.println("file1:"+f1);
System.out.println("file2:"+f2);
System.out.println("file3:"+f3);
System.out.println("file4:"+f4);
System.out.println("file5:"+f5);
System.out.println("file6:"+f6);
System.out.println("file7:"+f7);

}


}

执行结果为

file:true
file1:false
file2:false
file3:false
file4:false
file5:false
file6:false
file7:false

0 0
原创粉丝点击