Java小程序不支持Authenticator.setDefault
我想在Java小程序中执行简单的HTTP身份验证,我正在尝试以下方法:Java小程序不支持Authenticator.setDefault,java,security,jakarta-ee,applet,accesscontrolexception,Java,Security,Jakarta Ee,Applet,Accesscontrolexception,我想在Java小程序中执行简单的HTTP身份验证,我正在尝试以下方法: static class MyAuthenticator extends Authenticator { public PasswordAuthentication getPasswordAuthentication() { // I haven't checked getRequestingScheme() here, sinc
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小程序中对网站进行身份验证吗?您遇到了安全沙箱限制。显然,不受信任的小程序更改默认验证器是一个安全问题。(我想这是因为一个讨厌的小程序可以利用它窃取用户提供的身份验证详细信息。)
无论限制的原因是什么,一种解决方案是为小程序的JAR文件签名。有关详细信息,请参阅Oracle教程的一部分。是否有任何命令/方法/类可以更改特定小程序的安全参数?小程序中没有。这将破坏安全沙箱的目的。(问:讨厌的小程序要做的第一件事是什么?答:关闭安全检查以防止它做讨厌的事情!)那么有没有其他方法可以在java小程序中进行基本Http身份验证?我相信您可以通过设置适当的头来进行基本身份验证(根据RFC)使用硬连接的用户名和密码。
Authenticator
API的要点(通常)是要求用户提供信息。但是,如果不受信任的小程序被允许通过用户界面要求用户告知信息应该被信任,那么这显然是一个安全故障。在共享环境中修改全局状态是一个非常糟糕的主意(在其他代码中也不是一个好主意)。