⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guiproxyauthenticator.java

📁 weblogic应用全实例
💻 JAVA
字号:
//定义这个类所在的包
package examples.security.proxy;
//定义这个类引入的其他类
import java.awt.Button;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Point;
import java.awt.TextComponent;
import java.awt.TextField;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import weblogic.common.ProxyAuthenticator;

/** 这个实例演示通过代理服务器访问其他站点。这是个图形界面程序实现了
 * weblogic.common.proxyAuthenticator接口。
 * 实现这个接口是为了使应用程序提供服务器代理的用户认证信息。
 * GUIProxyAuthenticator 类弹出对话框,提示输入用户名和密码。
 */
public class GUIProxyAuthenticator extends Frame
implements ProxyAuthenticator, ActionListener, WindowListener {
    //界面元素
    private Button ok, cancel;
    private TextField user, passwd;
    private Label loginPrompt, hostNport;
    private boolean okPressed = false;
    //设置图标
    private void setIconImage() {
	try {
	    java.net.URL url = getClass().getResource("/weblogic/graphics/W.gif");
	    if (url == null)
		return;
	    Toolkit tk = Toolkit.getDefaultToolkit();
	    Image img = tk.createImage((java.awt.image.ImageProducer) url.getContent());
	    if (img != null) {
		setIconImage(img);
	    }
	} catch (Exception ignore) {}
    }

    /** 
     * 这个必须有一个public类型的缺省构造方法
     */
    public GUIProxyAuthenticator() {
	super("Proxy Authentication Required");
	//Frame布局
	setLayout(new GridBagLayout());
	GridBagConstraints gbc = new GridBagConstraints();
	/* top,left,bottom,right */
	gbc.insets = new Insets(5,5,5,5);
	Font f = new Font("SansSerif", Font.PLAIN, 12);
	setBackground(Color.lightGray);
	setIconImage();

	/* 0th row: label for hostname + port */
	gbc.gridwidth = gbc.REMAINDER;
	gbc.gridheight = 1;
	gbc.fill = gbc.HORIZONTAL;
	gbc.anchor = gbc.CENTER;
	gbc.gridx = gbc.gridy = 0;
	gbc.weightx = gbc.weighty = 1.0;
	/* text will change when we get the actual host & port */
	hostNport = new Label("Proxy Host:Port:");
	hostNport.setFont(f);
	add(hostNport, gbc);

	/* 1st row: label for login prompt */
	gbc.gridx = 0;
	gbc.gridy++;
	/* text may change when we get the actual login prompt */
	loginPrompt = new Label("Proxy Authentication Required");
	loginPrompt.setFont(f);
	add(loginPrompt, gbc);

	/* 2nd row: user name */
	gbc.gridx = 0;
	gbc.gridy++;
	gbc.weighty = 0.0;
	gbc.anchor = gbc.EAST;
	gbc.gridwidth = 1;
	gbc.fill = gbc.NONE;
	f = new Font("SansSerif", Font.BOLD, 12);
	Label l = new Label("User name:");
	l.setFont(f);
	add(l, gbc);
	gbc.gridx++;
	gbc.anchor = gbc.WEST;
	gbc.fill = gbc.HORIZONTAL;
	user = new TextField("", 12);
	user.setBackground(Color.white);
	add(user, gbc);

	/* 3rd row: password */
	gbc.gridx = 0;
	gbc.gridy++;
	gbc.anchor = gbc.EAST;
	gbc.fill = gbc.NONE;
	l = new Label("Password:");
	l.setFont(f);
	add(l, gbc);
	gbc.gridx++;
	gbc.anchor = gbc.WEST;
	gbc.fill = gbc.HORIZONTAL;
	passwd = new TextField("", 12);
	passwd.setEchoChar('*');
	passwd.setBackground(Color.white);
	add(passwd, gbc);

	/* 4th row: ok + cancel buttons */
	gbc.gridx = 0;
	gbc.gridy++;
	gbc.anchor = gbc.EAST;
	gbc.fill = gbc.NONE;
	ok = new Button("OK");
	ok.setFont(f);
	ok.addActionListener(this);
	add(ok, gbc);
	gbc.gridx++;
	gbc.anchor = gbc.WEST;
	cancel = new Button("Cancel");
	cancel.setFont(f);
	cancel.addActionListener(this);
	add(cancel, gbc);

	addWindowListener(this);
    }
    //初始化方法,在main方法中被调用
    public void init(String proxyHost, int proxyPort, String authType,
		     String promptString) {
	if (authType != null) {
	    setTitle('"' + authType + "\" Proxy Authentication Required");
	}
	hostNport.setText("Proxy Host:Port " + proxyHost + ':' + proxyPort);
	loginPrompt.setText('"' + promptString + '"');
    }
    //从用户输入获取登录名和密码
    public synchronized String[] getLoginAndPassword() {
      /* pack, and figure out the size the frame wants to be */
      pack();
      Dimension d = getSize();
      /* front & center */
      Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
      int xt = (screen.width - d.width)/2;
      int yt = (screen.height - d.height)/2;
      setLocation(new Point(xt, yt));
      setVisible(true);
      toFront();
      
      while (isVisible()) {
        try {
          wait();
        } catch (InterruptedException e) {
        }
      }
      
      /* clean up GUI resources */
      dispose();
      if (!okPressed)
        return null;
      
      String u = user.getText();
      String p = passwd.getText();
      if (u == null || u.length() == 0 ||
          p == null || p.length() == 0) {
        return null;
      }
      
      String[] ret = new String[2];
      ret[0] = u;
      ret[1] = p;
      return ret;
    }
  
  /* 事件响应方法 */
  public synchronized void actionPerformed(ActionEvent ev) {
    Object o = ev.getSource();
    if (o == ok) {
      okPressed = true;
      setVisible(false);
      notifyAll();
    } else if (o == cancel) {
      okPressed = false;
      setVisible(false);
      notifyAll();
    }
  }
  
  /* WindowListener监听事件方法 */
  public void windowActivated(WindowEvent e) { }
  public void windowClosed(WindowEvent e) {}
  public synchronized void windowClosing(WindowEvent e) {
    okPressed = false;
    setVisible(false);
    notifyAll();
  }
  
  public void windowDeactivated(WindowEvent e) {}
  public void windowDeiconified(WindowEvent e) {}
  public void windowIconified(WindowEvent e) {}
  public void windowOpened(WindowEvent e) {}
  
  /* 测试主方法 */
  public static void main(String[] a) {
    GUIProxyAuthenticator gpa = new GUIProxyAuthenticator();
    gpa.init("dumbarton", 8080, "Basic", "Authenticate with proxy");
    String[] unp = gpa.getLoginAndPassword();
    if (unp == null) {
      System.out.println("no authentication info provided");
    } else {
      System.out.println("username '" + unp[0] + "' passwd '" + unp[1] + "'");
    }
    System.exit(0);
  }
}


    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -