分享

Java API:Authenticator 类

 free_light 2014-02-24
  您用喜好的浏览器在网上冲浪时,您会遇到要求代理服务器认证或 HTTP 服务器认证的 URL,并会出现您再熟悉不过的窗口要求您输入用户名及口令: 
  
  从浏览器访问一个诸如 http://www./cgi-bin/Quotes/quote 这样的 URL 不成问题,因为您自己可以提供用户名和口令。但是当您试图通过 Java 程序从与此 URL 相关的 InputStream 中读取数据时,该 Java 程序就会发出 FileNotFoundException 异常。
从Java 1.2开始 ,在 java.net 包中添加了一个 Authenticator 类,这使得访问口令保护的 URL 变得极为容易。
  
  现在,对于 Java 1.2,您只需用 Authenticator.setDefault() 安装一个 Authenticator。这样,当需要认证时,已安装的 Authenticator 的 getPasswordAuthentication() 方法就会被调用,然后您就可以用适当的用户名和口令来设置 PasswordAuthentication 实例。
  
  所需步骤如下所示。
  
  第一步:安装 Authenticator
  
  Authenticator.setDefault (new MyAuthenticator ());
  
  第二步:创建 Authenticator 的子类
  
  class MyAuthenticator extends Authenticator {
  protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication ("username", "password");
  }
  }
  
  以下程序完整地显示了这种行为,它提供了自己的认证提示对话框。
  
  import java.io.*;
  import java.net.*;
  import java.awt.*;
  import java.awt.event.*;
  
  public class URLPassword extends Frame {
  
  private TextField tf = new TextField();
  private TextArea ta = new TextArea();
  
  public URLPassword() {
  
  super ("URL Password");
  
  // 安装 Authenticator
  Authenticator.setDefault (new MyAuthenticator ());
  
  // 设置屏幕
  add (tf, BorderLayout.NORTH);
  ta.setEditable(false);
  add (ta, BorderLayout.CENTER);
  tf.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
  String s = tf.getText();
  if (s.length() != 0)
  ta.setText (fetchURL (s));
  }
  });
  addWindowListener (new WindowAdapter() {
  public void windowClosing (WindowEvent e) {
  dispose();
  System.exit(0);
  }
  });
  }
  
  private String fetchURL (String urlString) {
  StringWriter sw = new StringWriter();
  PrintWriter pw = new PrintWriter(sw);
  
  try {
  URL url = new URL (urlString);
  InputStream content = (InputStream)url.getContent();
  BufferedReader in =
  new BufferedReader (new InputStreamReader (content));
  String line;
  while ((line = in.readLine()) != null) {
  pw.println (line);
  }
  } catch (MalformedURLException e) {
  pw.println ("Invalid URL");
  } catch (IOException e) {
  pw.println ("Error reading URL");
  }
  return sw.toString();
  }
  
  public static void main (String args[]) {
  Frame f = new URLPassword();
  f.setSize(300, 300);
  f.setVisible (true);
  }
  
  class MyAuthenticator extends Authenticator {
  protected PasswordAuthentication getPasswordAuthentication() {
  final Dialog jd = new Dialog (URLPassword.this, "Enter password", true);
  jd.setLayout (new GridLayout (0, 1));
  Label jl = new Label (getRequestingPrompt());
  jd.add (jl);
  TextField username = new TextField();
  username.setBackground (Color.lightGray);
  jd.add (username);
  TextField password = new TextField();
  password.setEchoChar ('*');
  password.setBackground (Color.lightGray);
  jd.add (password);
  Button jb = new Button ("OK");
  jd.add (jb);
  jb.addActionListener (new ActionListener() {
  public void actionPerformed (ActionEvent e) {
  jd.dispose();
  }
  });
  jd.pack();
  jd.setVisible(true);
  return new PasswordAuthentication (username.getText(), password.getText());
  }
  }
  }
【责编:admin】              

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多