首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么禁用反射机制

2013-07-01 
如何禁用反射机制比如我有两个类:public class B {}class A {private void test() {//Here do something}}

如何禁用反射机制
比如我有两个类:
public class B {
}
class A {
  private void test() {
  //Here do something
  }
}
在类B中,如果通过反射机制,是可以调用A中的私有方法,或者修改A的私有成员,怎么样禁止在B中使用反射来修改A中的东西呢?在网上查了下,说是用SecrityManager,但是具体怎么样用呢?望高人help me!非常感谢
看过,楼楼的类名好像写错了,应该是SecurityManager这个类,它有一个checkMemberAccess这个方法可以阻止你利用反射;
如:
 SecurityManager sm = new SecurityManager();
 sm.checkMemberAccess(Test.class, Member.PUBLIC);

前面一个为CLASS,后面需要填一个INT值,Member.PUBLIC 代表可以访问,如果是PUBLIC的话你的反射还是依然可以执行,还有一个值为DECLARED,然后再运行的时候,就会报错了。。。。。
[解决办法]
noraml access using java Reflection


import java.lang.reflect.Field;

public class UseReflection {
    public static void main(String args[]) {
        Object prey = new Prey();
        try {
            Field pf = prey.getClass().getDeclaredField("privateString");
            pf.setAccessible(true);
            pf.set(prey, "Aminur test");
            System.out.println(pf.get(prey));
        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

    }
}

class Prey {
    private String privateString = "privateValue";
}


the following example is using SecurityManager to prevent the java Reflection

import java.lang.reflect.Field;
import java.security.Permission;

public class UseReflection {
    static {
        try {
            System.setSecurityManager(new MySecurityManager());
        } catch (SecurityException se) {
            System.out.println("SecurityManager already set!");
        }

    }

    public static void main(String args[]) {
        Object prey = new Prey();
        try {
            Field pf = prey.getClass().getDeclaredField("privateString");


            pf.setAccessible(true);
            pf.set(prey, "Aminur test");
            System.out.println(pf.get(prey));
        } catch (Exception e) {
            System.err.println("Caught exception " + e.toString());
        }

    }
}

class Prey {
    private String privateString = "privateValue";
}

class MySecurityManager extends SecurityManager {
    public void checkPermission(Permission perm) {
        if (perm.getName().equals("suppressAccessChecks")) {
            throw new SecurityException("Can not change the permission dude.!");
        }
    }
}


[解决办法]
看过,楼楼的类名好像写错了,应该是SecurityManager这个类,它有一个checkMemberAccess这个方法可以阻止你利用反射;
如:
 SecurityManager sm = new SecurityManager();
 sm.checkMemberAccess(Test.class, Member.PUBLIC);

前面一个为CLASS,后面需要填一个INT值,Member.PUBLIC 代表可以访问,如果是PUBLIC的话你的反射还是依然可以执行,还有一个值为DECLARED,然后再运行的时候,就会报错了。。。。。



还是那句话, 先考虑应用场景!

假如class文件是在用户手里运行的,他完全可以在启动JVM 的时候用-Djava.security.manager 来加载自己写的SecurityManager, 然后在policy里不给你createSecurityManager的权限,这样你就不能new SecurityManager 了。 

或者他在启动时使用自定义 SystemClassLoader, 直接对所有你写的Class,在加载时进行字节码扫描,发现private的东西通通改成public,或者发现你试图new SecurityManager 就插入自定义的SecurityManager,这样仍然可以绕过检查。

总之如果class落到用户手里,略懂Java的人总有办法攻破你。
如果class是在server端运行,嗯,那你还检查个什么劲?信不过自己人?




[解决办法]
add vm parameter -Djava.security.manager 
the detail refer to http://aminurrashid.com/english/java/core-java/prevent-reflection-to-access-private-methods-and-members-in-java-classes.html

热点排行