java 反射 安全_java – 反射安全

嗯,它确实适用于setAccessible.看到:

class A {

private String method1() {

return "Hello World!";

}

}

import java.lang.reflect.Method;

class B {

public static void main(String[] args) throws Exception {

System.setSecurityManager(new SecurityManager());

Class clazz = A.class;

Method m = clazz.getDeclaredMethod("method1");

m.setAccessible(true);

}

}

结果是

Exception in thread "main" java.security.AccessControlException: access denied ("java.lang.reflect.ReflectPermission" "suppressAccessChecks")

at java.security.AccessControlContext.checkPermission(Unknown Source)

at java.security.AccessController.checkPermission(Unknown Source)

at java.lang.SecurityManager.checkPermission(Unknown Source)

at java.lang.reflect.AccessibleObject.setAccessible(Unknown Source)

at B.main(B.java:8)

它可能对你不起作用的一个原因是,根据this post中的评论,它不能用于Java 1.5,但在6和之后工作.

编辑:要为特定的罐子拒绝它,您需要使用策略文件,例如:

// specific file

grant codeBase "file:/test/path/tools.jar" {

// no permissions for this one

};

// default to giving all

grant {

permission java.security.AllPermission;

};

有两种方法可以指定策略文件,或者将其作为默认值添加,或者仅提供指定的策略文件(source):

If you use

06004

(note the double equals) then just the specified policy file will be

used; all the ones indicated in the security properties file will be

ignored.

…或者实现自定义安全管理器,doesn’t look that hard.尽管我自己也没有这样做.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值