Java applet不支持Authenticator.setDefault

时间:2012-06-07 13:50:00

标签: java security java-ee applet accesscontrolexception

我想在Java applet中进行简单的HTTP身份验证,我正在尝试这种方式:

static class MyAuthenticator extends Authenticator
        {
            public PasswordAuthentication getPasswordAuthentication()
                {
            // I haven't checked getRequestingScheme() here, since for NTLM
            // and Negotiate, the usrname and password are all the same.
            System.err.println("Feeding username and password for " + getRequestingScheme());
            return (new PasswordAuthentication(kuser, kpass.toCharArray()));
            }
        }
    public void paint(Graphics g) {
        try
        {
        //  String authenticate=Authenticator.getPasswordAuthentication();
             Authenticator.setDefault(new MyAuthenticator());
             URL url = new URL("http://Ip_address/jpg/image.jpg");
             InputStream ins = url.openConnection().getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
             String str;
               while((str = reader.readLine()) != null)
                  System.out.println(str);

          //int s=2+2;

            g.drawString("hello world", 50, 60 );
        }
        catch (Exception e)
        {
            System.out.println("Exception : "+ e);
        }
    }

但我在这一行得到错误

Authenticator.setDefault(new MyAuthenticator());

例外是:

Exception : java.security.AccessControlException: access denied 
    (java.net.NetPermission setDefaultAuthenticator)

有人能告诉我现在要做什么,或者如何从Java applet中验证网站内部?

1 个答案:

答案 0 :(得分:1)

您遇到了安全沙箱限制。显然,不受信任的applet更改默认身份验证器是一个安全问题。 (我想这是因为一个讨厌的applet可以使用它来窃取用户提供的身份验证细节。)

无论限制的原因是什么,一种解决方案是为您的applet签署JAR文件。有关详细信息,请参阅Oracle教程的this page