Java中禁止的包名(Prohibited package name)

阅读更多
由于定义了以java开始的包(java.mypackage),编译时错误:
java.lang.SecurityException: Prohibited package name: java.mypackage
	at java.lang.ClassLoader.preDefineClass(ClassLoader.java:479)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:614)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
	at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
	at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
Exception in thread "main"


根据异常信息,定位到java.lang.ClassLoader.preDefineClass进行排查,发现以下代码片断:
/* Determine protection domain, and check that:
        - not define java.* class,
        - signer of this class matches signers for the rest of the classes in package.
*/
    private ProtectionDomain preDefineClass(String name,
	ProtectionDomain protectionDomain)
    {
	if (!checkName(name))
	    throw new NoClassDefFoundError("IllegalName: " + name);
	if ((name != null) && [color=red]name.startsWith("java.")[/color]) {
	    throw new SecurityException("Prohibited package name: " +
			name.substring(0, name.lastIndexOf('.')));
	}
	if (protectionDomain == null) {
	    protectionDomain = getDefaultDomain();
	}

	if (name != null)
	    checkCerts(name, protectionDomain.getCodeSource());

	return protectionDomain;
    }

......

// true if the name is null or has the potential to be a valid binary name
    private boolean checkName(String name) {
	if ((name == null) || (name.length() == 0))
   	    return true;
	if ((name.indexOf('/') != -1)
	    || (!VM.allowArraySyntax() && (name.charAt(0) == '[')))
   	    return false;
 	return true;
    }


可以看出preDefineClass方法首先对类名进行了检查,发现以java作为一级包名,则抛出安全异常:禁止使用的包名!

这条安全异常是由Java类加载的“双亲委派模型”(详见 这里)所导致的。在双亲委派模型中,由父加载类加载的类,下层加载器是不能加载的。本例中最高层加载器BootstrapClassLoader加载了classpath路径下所定义的java.*包内的类,而java.research包就不能由BootstrapClassLoader的下层加载器AppClassLoader加载了。这也是java安全机制中对于恶意代码所采取的防护措施。

另参见: http://dev.csdn.net/develop/article/45/article/39/39272.shtm

你可能感兴趣的:(Java,.net,Security,REST,Access)