1 package org.jdesktop.swing.auth; 2 3 9 10 import javax.security.auth.callback.Callback ; 11 import javax.security.auth.callback.CallbackHandler ; 12 import javax.security.auth.callback.NameCallback ; 13 import javax.security.auth.callback.PasswordCallback ; 14 import javax.security.auth.login.AccountExpiredException ; 15 import javax.security.auth.login.CredentialExpiredException ; 16 import javax.security.auth.login.FailedLoginException ; 17 import javax.security.auth.login.LoginContext ; 18 import javax.security.auth.login.LoginException ; 19 20 27 public class JAASLoginService extends LoginService { 28 29 33 public JAASLoginService(String server) { 34 super(server); 35 } 36 37 38 42 public boolean authenticate(String name, char[] password, String server) { 43 try { 44 LoginContext loginContext = null; 45 46 loginContext = new LoginContext (getServer(), 47 new JAASCallbackHandler(name, password)); 48 loginContext.login(); 49 return true; 50 } catch (AccountExpiredException e) { 51 e.printStackTrace(); 53 return false; 54 } catch (CredentialExpiredException e) { 55 e.printStackTrace(); 57 return false; 58 } catch (FailedLoginException e) { 59 e.printStackTrace(); 61 return false; 62 } catch (LoginException e) { 63 e.printStackTrace(); 65 return false; 66 } catch (Throwable e) { 67 e.printStackTrace(); 69 return false; 70 } 71 } 72 73 class JAASCallbackHandler implements CallbackHandler { 74 75 private String name; 76 77 private char[] password; 78 79 public JAASCallbackHandler(String name, char[] passwd) { 80 this.name = name; 81 this.password = passwd; 82 } 83 84 public void handle(Callback [] callbacks) throws java.io.IOException { 85 for (int i = 0; i < callbacks.length; i++) { 86 if (callbacks[i] instanceof NameCallback ) { 87 NameCallback cb = (NameCallback ) callbacks[i]; 88 cb.setName(name); 89 } else if (callbacks[i] instanceof PasswordCallback ) { 90 PasswordCallback cb = (PasswordCallback ) callbacks[i]; 91 cb.setPassword(password); 92 } 93 } 94 } 95 96 } 97 98 99 } 100 | Popular Tags |