【问题标题】:Authenticator getPasswordAuthentication not called [closed]未调用身份验证器 getPasswordAuthentication [关闭]
【发布时间】:2015-04-16 00:04:07
【问题描述】:

我是网络编程的新手。 这是我的代码:

public class ComputerNetworks extends Authenticator {
    private JDialog passwordDialog;
    private JLabel mainLabel= new JLabel("Please enter username and password: ");
    private JLabel userLabel = new JLabel("Username: ");
    private JLabel passwordLabel = new JLabel("Password: ");
    private JTextField usernameField = new JTextField(20);
    private JPasswordField passwordField = new JPasswordField(20);
    private JButton okButton = new JButton("OK");
    private JButton cancelButton = new JButton("Cancel");

    public ComputerNetworks( ) {
        this("", new JFrame());
    }

    public ComputerNetworks(String username) {
        this(username, new JFrame());
    }

    public ComputerNetworks(JFrame parent) {
        this("", parent);
    }

    public ComputerNetworks(String username, JFrame parent) {
        this.passwordDialog = new JDialog(parent, true);
        Container pane = passwordDialog.getContentPane( );
        pane.setLayout(new GridLayout(4, 1));
        pane.add(mainLabel);
        JPanel p2 = new JPanel( );
        p2.add(userLabel);
        p2.add(usernameField);
        usernameField.setText(username);
        pane.add(p2);
        JPanel p3 = new JPanel( );
        p3.add(passwordLabel);
        p3.add(passwordField);
        pane.add(p3);
        JPanel p4 = new JPanel( );
        p4.add(okButton);
        p4.add(cancelButton);
        pane.add(p4);
        ActionListener al = new OKResponse( );
        okButton.addActionListener(al);
        usernameField.addActionListener(al);
        passwordField.addActionListener(al);
        cancelButton.addActionListener(new CancelResponse( ));
        passwordDialog.pack( );
        passwordDialog.setVisible(true);

    }

    private void show( ) {
        String prompt = this.getRequestingPrompt( );
        if (prompt == null) {
            String site = this.getRequestingSite().getHostName( );
            String protocol = this.getRequestingProtocol( );
            int port = this.getRequestingPort();
            if (site != null & protocol != null) {
                prompt = protocol + "://" + site;
                if (port > 0) 
                    prompt += ":" + port;
            } else {
                prompt = "";
            }
        }
        mainLabel.setText("Please enter username and password for "
            + prompt + ": ");
        passwordDialog.pack();
        passwordDialog.setVisible(true);
    }


    PasswordAuthentication response = null;

    class OKResponse implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("OK clicked");
            passwordDialog.setVisible(false);
            // The password is returned as an array of
            // chars for security reasons.
            char[] password = passwordField.getPassword( );
            String username = usernameField.getText( );
            // Erase the password in case this is used again.
            passwordField.setText("");
            response = new PasswordAuthentication(username, password);
        }
    }

    class CancelResponse implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            passwordDialog.hide( );
            // Erase the password in case this is used again.
            passwordField.setText("");
            response = null;
        }
    }

    public PasswordAuthentication getPasswordAuthentication( ) {
        this.show();
        return this.response;
    }

    public static void main(String[] args) throws Exception {
        Authenticator.setDefault(new ComputerNetworks());
        URL u = new URL("http://www.google.co.in");
        InputStream in = u.openStream();
    }
}

我正在从 IDE 运行此代码。问题是AuthenticatorgetPasswordAuthentication() 方法没有被调用,尽管我已经打开了一个带有URL 的流。 请帮忙。 提前致谢

【问题讨论】:

  • 您对代码缩进的概念也很陌生吗?这是您成功的关键。
  • 请使用 IDE(大 3 中的任何一个都是不错的选择)和自动格式化功能。不管这段代码是什么,它都难以辨认。
  • 格式化问题。请帮忙

标签: java authentication network-programming authenticator


【解决方案1】:

来自JavaDoc

getPasswordAuthentication()

protected PasswordAuthentication getPasswordAuthentication() 当需要密码授权时调用。子类应该覆盖默认实现,它返回 null。 回报: 从用户那里收集的 PasswordAuthentication,如果没有提供,则返回 null。

据我所知,google.co.in 不需要密码授权。因此,不使用Authenticator。尝试输入一个确实需要身份验证的 URL,看看是否有帮助。

【讨论】:

  • 先生,您知道此类网站的任何示例吗?找不到
  • @user2653926 “这样的网站”?我不确定你是什么意思。编辑:您显然是指受密码保护的密码,我的错。如果你在家,你可以试试你的路由器。
  • 需要基本身份验证的 URL
  • 我用我的路由器尝试过,但密码对话框不断重新出现。我尝试输入我的 wifi 用户名和密码
  • 您的路由器有自己的用户名和密码。查看手册或在线查找型号。大多数都有一个默认组合,如 admin/admin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多