jre8 中 URLPermission 的 IllegalArgumentException [英] IllegalArgumentException at URLPermission in jre8

查看:20
本文介绍了jre8 中 URLPermission 的 IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行下面的代码时,

when i run the code below,

-在 JRE8 上的小程序中,在 con.getInputStream() 行上,它抛出异常

-in an Applet on JRE8, on the line con.getInputStream() it throws the exception

-在 JRE7 或 JRE6 上的小程序中它不会抛出.

-in an Applet on JRE7 or JRE6 it does not throws.

-在任何 JRE 上的桌面应用程序中它不会抛出.

-in a Desktop app on any JRE it does not throws.

当我删除以 setRequestPropery 开头的行时,它不会在任何 JRE 上抛出异常.

when i remove the lines starts with setRequestPropery, it does not throws the exception on any JRE.

        URLConnection con = new URL(adress).openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type",
                "application/octet-stream");
        con.setRequestProperty("pragma:", "no-cache");
        PrintStream ps = new PrintStream(con.getOutputStream());
        ps.println("Test");
        ps.close();
        in = new DataInputStream(conn.getInputStream());

异常:

java.lang.IllegalArgumentException: invalid actions string
at java.net.URLPermission.init(Unknown Source)
at java.net.URLPermission.<init>(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.URLtoSocketPermission(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)

在我的小程序中,我试图打开一个连接并且我需要这些请求属性.

In my applet, i am trying to open a connection and i need those request properties.

你知道是什么导致了 JRE8 上的这个异常吗?以及为什么只在小程序中而不是在桌面应用程序中.

推荐答案

在小程序中调试您的代码片段,显示参数 actions 传递给 URLPermission 是新的 win java8,其值为 GET:pragma: 根据 javadoc 对于该参数无效:

Debugin your code snippet in an applet, shows that the parameter actions passed to URLPermission, which ist new win java8, has the value GET:pragma: which is not valid according to javadoc for that argument:

URLPermission 的操作字符串是方法的串联列表和请求头列表.这些是允许的列表权限的请求方法和允许的请求头(分别).这两个列表由冒号:"字符分隔和每个列表的元素以逗号分隔.一些例子是:

The actions string of a URLPermission is a concatenation of the method list and the request headers list. These are lists of the permitted request methods and permitted request headers of the permission (respectively). The two lists are separated by a colon ':' character and elements of each list are comma separated. Some examples are:

     "POST,GET,DELETE"
     "GET:X-Foo-Request,X-Bar-Request"
     "POST,GET:Header1,Header2"

并根据oracle的jdk8中的代码:

and according to the code in oracle's jdk8:

int colon = actions.indexOf(':');
if (actions.lastIndexOf(':') != colon) {
    throw new IllegalArgumentException("invalid actions string");
}

上面的代码需要一个冒号或没有冒号.

The code above expects one single colon or none.

要解决此问题,您需要在调用中将pragma 后的冒号去掉

To solve the problem you need to remove the colon after pragma in your call

con.setRequestProperty("pragma", "no-cache");

将代码片段作为简单的 junit 测试运行不会引发此异常,因为未调用 URLPermition 类.是否调用取决于应用程序运行的上下文.

Running your snippet as simple junit test does not provoke this exception, because the URLPermition class is not invoked. Whether it invoked or not depends on the context where the application is running.

注意.根据使用的上下文,某些请求方法和标头可能在任何时候都允许,而其他的可能在任何时候都不允许.例如,HTTP 协议处理程序可能不允许应用程序代码设置某些标头(如 Content-Length),而不管有效的安全策略是否允许.

Note. Depending on the context of use, some request methods and headers may be permitted at all times, and others may not be permitted at any time. For example, the HTTP protocol handler might disallow certain headers such as Content-Length from being set by application code, regardless of whether the security policy in force, permits it.

看来,当在小程序的上下文中时,会执行一些许可检查.

So it seems, when in the context of an applet some permition checks are performed.

这篇关于jre8 中 URLPermission 的 IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆