KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > auth > JAASLoginService


1 package org.jdesktop.swing.auth;
2
3 /*
4  * $Id: JAASLoginService.java,v 1.2 2005/01/28 00:48:07 bino_george Exp $
5  *
6  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
7  * Santa Clara, California 95054, U.S.A. All rights reserved.
8  */

9
10 import javax.security.auth.callback.Callback JavaDoc;
11 import javax.security.auth.callback.CallbackHandler JavaDoc;
12 import javax.security.auth.callback.NameCallback JavaDoc;
13 import javax.security.auth.callback.PasswordCallback JavaDoc;
14 import javax.security.auth.login.AccountExpiredException JavaDoc;
15 import javax.security.auth.login.CredentialExpiredException JavaDoc;
16 import javax.security.auth.login.FailedLoginException JavaDoc;
17 import javax.security.auth.login.LoginContext JavaDoc;
18 import javax.security.auth.login.LoginException JavaDoc;
19
20 /**
21  * <b>JAASLoginService</b> implements a <b>LoginService</b>
22  * that uses JAAS for authentication. <b>JAASLoginService</b> uses the
23  * server name as name of the configuration for JAAS.
24  *
25  * @author Bino George
26  */

27 public class JAASLoginService extends LoginService {
28
29     /**
30      * Constructor for <b>JAASLoginService</b>
31      * @param server server name that is also used for the JAAS config name
32      */

33     public JAASLoginService(String JavaDoc server) {
34         super(server);
35     }
36     
37     
38     /**
39      * @inheritDoc
40      *
41      */

42     public boolean authenticate(String JavaDoc name, char[] password, String JavaDoc server) {
43         try {
44             LoginContext JavaDoc loginContext = null;
45
46             loginContext = new LoginContext JavaDoc(getServer(),
47                     new JAASCallbackHandler(name, password));
48             loginContext.login();
49             return true;
50         } catch (AccountExpiredException JavaDoc e) {
51             // TODO log
52
e.printStackTrace();
53             return false;
54         } catch (CredentialExpiredException JavaDoc e) {
55             // TODO log
56
e.printStackTrace();
57             return false;
58         } catch (FailedLoginException JavaDoc e) {
59             // TODO log
60
e.printStackTrace();
61             return false;
62         } catch (LoginException JavaDoc e) {
63             // TODO log
64
e.printStackTrace();
65             return false;
66         } catch (Throwable JavaDoc e) {
67             // TODO log
68
e.printStackTrace();
69             return false;
70         }
71     }
72
73     class JAASCallbackHandler implements CallbackHandler JavaDoc {
74
75         private String JavaDoc name;
76
77         private char[] password;
78
79         public JAASCallbackHandler(String JavaDoc name, char[] passwd) {
80             this.name = name;
81             this.password = passwd;
82         }
83
84         public void handle(Callback JavaDoc[] callbacks) throws java.io.IOException JavaDoc {
85             for (int i = 0; i < callbacks.length; i++) {
86                 if (callbacks[i] instanceof NameCallback JavaDoc) {
87                     NameCallback JavaDoc cb = (NameCallback JavaDoc) callbacks[i];
88                     cb.setName(name);
89                 } else if (callbacks[i] instanceof PasswordCallback JavaDoc) {
90                     PasswordCallback JavaDoc cb = (PasswordCallback JavaDoc) callbacks[i];
91                     cb.setPassword(password);
92                 }
93             }
94         }
95
96     }
97
98     
99 }
100
Popular Tags