有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

异常setDefaultAuthenticator导致java错误。安全GWT web应用程序中的AccessControlException

我正在使用GWT创建一个web应用程序,服务器向客户机提供服务。在该服务中,它必须向另一台具有身份验证的服务器生成HTTP请求。以下代码描述了如何发出请求:

public int request()
{
    Authenticator.setDefault(new MyAuthenticator(username, password));
    try {
        URL url = new URL(url_string);
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("Content-Type", "text/xml");
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(buildRequest());
        wr.flush();
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        //the rest is to handle the response ...

    } catch (Exception e) {
        //handle exception
    }
}

MyAuthenticator是按照建议的方式实现的here,我测试了代码,它工作起来很有魅力,但当我尝试将其纳入GWT项目时,我遇到了一个异常:

//huge stack trace above ... what is important is the following
Caused by: java.security.AccessControlException: access denied (java.net.NetPermission setDefaultAuthenticator)

有什么办法解决这个问题吗


共 (2) 个答案

  1. # 1 楼答案

    我可以看到两种情况:

    1. 您正在使用AppEngine运行它。我相信这是行不通的
    2. 您正在GWT的客户端上使用它。将其移动到服务器端
  2. # 2 楼答案

    显然,GAE不允许验证器,这就是我得到这个异常的原因,发现了一个类似的问题here,解决方案是按照建议here手动添加认证头