0

Javaアプレットで単純な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アプレットから内部のWebサイトを認証する方法を誰か教えてもらえますか?

4

1 に答える 1

1

セキュリティ サンドボックスの制限に遭遇しました。どうやら、信頼されていないアプレットがデフォルトのオーセンティケータを変更することは、セキュリティ上の問題です。(厄介なアプレットがこれを使用して、ユーザーから提供された認証の詳細を盗む可能性があるためだと思います。)

制限の理由が何であれ、1 つの解決策は、アプレットの JAR ファイルに署名することです。詳細については、Oracle チュートリアルのこのページを参照してください。

于 2012-06-07T14:14:05.993 に答える