Java/Security/Permission
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
- 1 boolean Permission.implies(Permission permission)
- 2 Checking Read/Write Permission for a Directory
- 3 Controlling Access to an Object using a permission
- 4 Creating a Custom Permission
- 5 Determining If One Permission Implies Another
- 6 extends BasicPermission
- 7 Guard guard = new PropertyPermission("java.home", "read");
- 8 Listing All Permissions Granted to a Loaded Class
- 9 new GuardedObject(Object obj, Guard)
- 10 Object GuardedObject.getObject()
boolean Permission.implies(Permission permission)
import java.io.Serializable;
import java.security.BasicPermission;
import java.security.Permission;
class IDPermission extends BasicPermission implements Serializable {
String id = null;
public IDPermission(String id) {
super(id);
this.id = id;
}
public boolean implies(Permission permission) {
IDPermission bp = (IDPermission) permission;
return id.equals(bp.id);
}
public String getActions() {
return "";
}
public int hashCode() {
return id.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof IDPermission)) {
return false;
}
IDPermission bp = (IDPermission) obj;
return id.equals(bp.id);
}
}
public class Main {
public static void main(String[] argv) throws Exception {
Permission p1 = new IDPermission("1");
Permission p2 = new IDPermission("2");
boolean b = p1.implies(p2);
p2 = new IDPermission("3");
b = p1.implies(p2);
p2 = new IDPermission("4");
b = p1.implies(p2);
p2 = new IDPermission("5");
b = p1.implies(p2);
p2 = new IDPermission("6");
b = p1.implies(p2);
}
}
Checking Read/Write Permission for a Directory
import java.io.FilePermission;
import java.security.AccessController;
public class Main {
public static void main(String[] argv) throws Exception {
AccessController.checkPermission(new FilePermission("/tmp/*", "read,write"));
}
}
Controlling Access to an Object using a permission
import java.security.AccessControlException;
import java.security.Guard;
import java.security.GuardedObject;
import java.util.PropertyPermission;
public class Main {
public static void main(String[] argv) throws Exception {
String secretObj = "secret";
Guard guard = new PropertyPermission("java.home", "read");
GuardedObject gobj = new GuardedObject(secretObj, guard);
try {
Object o = gobj.getObject();
} catch (AccessControlException e) {
e.printStackTrace();
}
}
}
Creating a Custom Permission
import java.io.Serializable;
import java.security.BasicPermission;
import java.security.Permission;
class IDPermission extends BasicPermission implements Serializable {
String id = null;
public IDPermission(String id) {
super(id);
this.id = id;
}
public boolean implies(Permission permission) {
IDPermission bp = (IDPermission) permission;
return id.equals(bp.id);
}
public String getActions() {
return "";
}
public int hashCode() {
return id.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof IDPermission)) {
return false;
}
IDPermission bp = (IDPermission) obj;
return id.equals(bp.id);
}
}
public class Main {
public static void main(String[] argv) throws Exception {
Permission p1 = new IDPermission("1");
Permission p2 = new IDPermission("2");
boolean b = p1.implies(p2);
p2 = new IDPermission("3");
b = p1.implies(p2);
p2 = new IDPermission("4");
b = p1.implies(p2);
p2 = new IDPermission("5");
b = p1.implies(p2);
p2 = new IDPermission("6");
b = p1.implies(p2);
}
}
Determining If One Permission Implies Another
import java.io.FilePermission;
import java.security.Permission;
public class Main {
public static void main(String[] argv) throws Exception {
Permission perm1 = new FilePermission("/tmp/*", "read,write");
Permission perm2 = new FilePermission("/tmp/abc", "read");
if (perm1.implies(perm2)) {
System.out.println("perm1 implies perm2");
}
}
}
extends BasicPermission
import java.io.Serializable;
import java.security.BasicPermission;
import java.security.Permission;
class IDPermission extends BasicPermission implements Serializable {
String id = null;
public IDPermission(String id) {
super(id);
this.id = id;
}
public boolean implies(Permission permission) {
IDPermission bp = (IDPermission) permission;
return id.equals(bp.id);
}
public String getActions() {
return "";
}
public int hashCode() {
return id.hashCode();
}
public boolean equals(Object obj) {
if (!(obj instanceof IDPermission)) {
return false;
}
IDPermission bp = (IDPermission) obj;
return id.equals(bp.id);
}
}
public class Main {
public static void main(String[] argv) throws Exception {
Permission p1 = new IDPermission("1");
Permission p2 = new IDPermission("2");
boolean b = p1.implies(p2);
p2 = new IDPermission("3");
b = p1.implies(p2);
p2 = new IDPermission("4");
b = p1.implies(p2);
p2 = new IDPermission("5");
b = p1.implies(p2);
p2 = new IDPermission("6");
b = p1.implies(p2);
}
}
Guard guard = new PropertyPermission("java.home", "read");
import java.security.AccessControlException;
import java.security.Guard;
import java.security.GuardedObject;
import java.util.PropertyPermission;
public class Main {
public static void main(String[] argv) throws Exception {
String secretObj = "secret";
Guard guard = new PropertyPermission("java.home", "read");
GuardedObject gobj = new GuardedObject(secretObj, guard);
try {
Object o = gobj.getObject();
} catch (AccessControlException e) {
e.printStackTrace();
}
}
}
Listing All Permissions Granted to a Loaded Class
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.util.Enumeration;
public class Main {
public static void main(String[] argv) throws Exception {
ProtectionDomain domain = String.class.getProtectionDomain();
PermissionCollection pcoll = Policy.getPolicy().getPermissions(domain);
Enumeration e = pcoll.elements();
for (; e.hasMoreElements();) {
Permission p = (Permission) e.nextElement();
}
}
}
new GuardedObject(Object obj, Guard)
import java.security.AccessControlException;
import java.security.Guard;
import java.security.GuardedObject;
import java.util.PropertyPermission;
public class Main {
public static void main(String[] argv) throws Exception {
String secretObj = "secret";
Guard guard = new PropertyPermission("java.home", "read");
GuardedObject gobj = new GuardedObject(secretObj, guard);
try {
Object o = gobj.getObject();
} catch (AccessControlException e) {
e.printStackTrace();
}
}
}
Object GuardedObject.getObject()
import java.security.AccessControlException;
import java.security.Guard;
import java.security.GuardedObject;
import java.util.PropertyPermission;
public class Main {
public static void main(String[] argv) throws Exception {
String secretObj = "secret";
Guard guard = new PropertyPermission("java.home", "read");
GuardedObject gobj = new GuardedObject(secretObj, guard);
try {
Object o = gobj.getObject();
} catch (AccessControlException e) {
e.printStackTrace();
}
}
}