Java 访问控制器使用
我试图了解java安全性和AccessController.doPrivileged()用法的基础知识 我从一个示例程序开始Java 访问控制器使用,java,security,Java,Security,我试图了解java安全性和AccessController.doPrivileged()用法的基础知识 我从一个示例程序开始 import java.security.AccessController; import java.security.PrivilegedAction; public class AccessSystemProperty { public static void main(String[] args) { System.out.println(Syste
import java.security.AccessController;
import java.security.PrivilegedAction;
public class AccessSystemProperty {
public static void main(String[] args) {
System.out.println(System.getSecurityManager());
AccessController.doPrivileged(
new PrivilegedAction<Boolean>(){
public Boolean run(){
System.out.println(System.getProperty("java.home"));
return Boolean.TRUE;
}
}
);
}
}
请帮我弄清楚
1) 当我们需要使用AccessController.doPrivileged()时?(如果存在SecurityManager,我们将使用AccessController.doPrivileged-为什么在上面的示例中失败)
2) 使用AccessController和PrivilegedAction的真正优势是什么
3) 我们是否需要上述示例的自定义策略文件才能工作
谢谢
Paul您可以使用AccessController.doPrivileged()来授予某些代码特权,这些特权是调用堆栈中早期的代码所没有的,但特权代码由于在策略中授予了该特权而拥有的 例如,假设ClassA调用ClassB上的方法,ClassB需要读取java.home系统属性(借用您的示例),并假设您已指定SecurityManager如您的示例所示 还假设ClassB是从名为“ClassB.jar”的jar加载的(但为了使示例工作,ClassA不是从该jar加载的),安全策略文件中应包含以下内容:
grant codeBase "file:/home/somebody/classb.jar" {
permission java.util.PropertyPermission "java.home", "read";
};
现在,当ClassB运行并尝试执行System.getProperty()时,它没有包装在“java.home”上的AccessController.doPrivileged()中。安全管理器将检查堆栈,以查看堆栈上较高的每个类是否都具有“java.home”的PropertyPermission(无论是直接的还是隐含的)。否则,访问将失败
但是,如果ClassB将System.getProperty()包装在AccessController.doPrivileged()中,则securitymanager只关心策略文件是否赋予ClassB该权限,从而允许访问
下面是一个片段来说明这一点:
public void doStuff() {
try {
/*
* this will fail even if this class has permission via the policy file
* IF any caller does not have permission
*/
System.out.println(System.getProperty("java.home"));
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
/*
* this will be allowed if this class has permission via the policy
* file even if no caller has permission
*/
System.out.println(System.getProperty("java.home"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return Boolean.TRUE;
}
});
public void doStuff(){
试一试{
/*
*即使此类通过策略文件具有权限,此操作也将失败
*如果任何调用方没有权限
*/
System.out.println(System.getProperty(“java.home”);
}捕获(异常e1){
System.out.println(e1.getMessage());
}
AccessController.doPrivileged(新的PrivilegedAction(){
公共布尔运行(){
试一试{
/*
*如果此类通过策略具有权限,则允许此操作
*即使没有调用者拥有权限,也要创建文件
*/
System.out.println(System.getProperty(“java.home”);
}捕获(例外e){
System.out.println(e.getMessage());
}
返回Boolean.TRUE;
}
});
因此,在您的示例中,您只需要指定一个策略文件,其中包含类似于我上面提到的grant节的内容。嗨,Rob,感谢您提供了如此清晰和详细的解释。我总是从单个类中尝试,因此无法找出区别。当我们启用默认安全管理器时(使用-Djava.security.manager)是否有任何用于限制访问的策略文件?(它是JDK的一部分)这里您是从Thread.currentThread().getStackTrace()引用StackTraceeElement[];或堆栈内存?。您能解释一下吗?@PaulErric默认策略文件在java.security文件中定义,通常是policy.url.1=文件:${java.home}/lib/security/java.policy.url.2=文件:${user.home}/.java.policy。此链接还有:。通过堆栈,我指的是可以在stacktraceelements中看到的方法调用序列。
public void doStuff() {
try {
/*
* this will fail even if this class has permission via the policy file
* IF any caller does not have permission
*/
System.out.println(System.getProperty("java.home"));
} catch (Exception e1) {
System.out.println(e1.getMessage());
}
AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
public Boolean run() {
try {
/*
* this will be allowed if this class has permission via the policy
* file even if no caller has permission
*/
System.out.println(System.getProperty("java.home"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return Boolean.TRUE;
}
});