我想在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内部验证网站?
发布于 2012-06-07 22:14:06
您遇到了安全沙箱限制。显然,不受信任的applet更改默认身份验证器是一个安全问题。(我想这是因为一个讨厌的applet可以使用它来窃取用户提供的身份验证详细信息。)
无论限制的原因是什么,一种解决方案是对applet的JAR文件进行签名。有关详细信息,请参阅Oracle教程的this page。
https://stackoverflow.com/questions/10933217
复制相似问题