1 4 package com.ca.commons.jndi; 5 6 import java.io.IOException ; 7 import java.awt.*; 8 9 import javax.security.auth.callback.Callback ; 10 import javax.security.auth.callback.CallbackHandler ; 11 import javax.security.auth.callback.NameCallback ; 12 import javax.security.auth.callback.PasswordCallback ; 13 import javax.security.auth.callback.UnsupportedCallbackException ; 14 import javax.swing.*; 15 16 22 public class JXCallbackHandler implements CallbackHandler 23 { 24 private static Frame owner = null; 29 private static String promptHeader = "Require Kerberos Credentials"; 32 33 41 public static void setupGUI(Frame ownerFrame, String userNamePromptString, String pwdPromptString, String promptHeaderString) 42 { 43 owner = ownerFrame; 44 promptHeader = promptHeaderString; 47 } 48 49 public JXCallbackHandler() 50 { 51 } 52 53 58 public void handle(Callback [] callbacks) throws IOException , 59 UnsupportedCallbackException 60 { 61 for (int i = 0; i < callbacks.length; i++) 62 { 63 64 if (callbacks[i] instanceof NameCallback ) 65 { 66 67 NameCallback cb = (NameCallback ) callbacks[i]; 68 cb.setName(getUserName(cb.getPrompt())); 69 } 70 else if (callbacks[i] instanceof PasswordCallback ) 71 { 72 PasswordCallback cb = (PasswordCallback ) callbacks[i]; 73 cb.setPassword(getPassword(cb.getPrompt())); 74 } 75 else 76 { 77 throw new UnsupportedCallbackException (callbacks[i]); 78 } 79 } 80 } 81 82 88 private String getUserName(String prompt) 89 { 90 String result = (String )JOptionPane.showInputDialog(owner, prompt, promptHeader, JOptionPane.QUESTION_MESSAGE); 91 return result; 92 } 93 94 95 101 private char[] getPassword(String prompt) 102 { 103 JPasswordField pwd = new JPasswordField(); 104 JPanel panel = new JPanel(new BorderLayout()); 105 panel.add(new JLabel(prompt), BorderLayout.NORTH); 106 panel.add(pwd); 107 108 JOptionPane.showMessageDialog(owner, panel, promptHeader, JOptionPane.QUESTION_MESSAGE); 109 return pwd.getPassword(); 110 } 111 112 116 public static void main(String [] args) 117 { 118 JXCallbackHandler test = new JXCallbackHandler(); 119 System.out.println("user name = " + test.getUserName("a nice shiny user name")); 120 System.out.println("pwd = " + new String (test.getPassword("a nice shiny password"))); 121 System.exit(0); 122 } 123 } 124 | Popular Tags |