KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > security > SecurityAssociationAuthenticator


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.security;
8
9 import java.net.Authenticator JavaDoc;
10 import java.net.PasswordAuthentication JavaDoc;
11 import java.security.Principal JavaDoc;
12 import java.security.AccessController JavaDoc;
13 import java.security.PrivilegedAction JavaDoc;
14
15 /** An implementation of Authenticator that obtains the username and password
16  * from the current SecurityAssociation state.
17  *
18  * @author Scott.Stark@jboss.org
19  * @version $Revision: 1.5 $
20  */

21 public class SecurityAssociationAuthenticator extends Authenticator JavaDoc
22 {
23    protected PasswordAuthentication JavaDoc getPasswordAuthentication()
24    {
25       SecurityActions sa = SecurityActions.UTIL.getSecurityActions();
26       Principal JavaDoc principal = sa.getPrincipal();
27       Object JavaDoc credential = sa.getCredential();
28       String JavaDoc name = principal != null ? principal.getName() : null;
29       char[] password = {};
30       if( credential != null )
31       {
32          if( password.getClass().isInstance(credential) )
33             password = (char[]) credential;
34          else
35             password = credential.toString().toCharArray();
36       }
37       PasswordAuthentication JavaDoc auth = new PasswordAuthentication JavaDoc(name, password);
38       return auth;
39    }
40
41    interface SecurityActions
42    {
43       class UTIL
44       {
45          static SecurityActions getSecurityActions()
46          {
47             return System.getSecurityManager() == null ? NON_PRIVILEGED : PRIVILEGED;
48          }
49       }
50
51       SecurityActions NON_PRIVILEGED = new SecurityActions()
52       {
53          public Principal JavaDoc getPrincipal()
54          {
55             return SecurityAssociation.getPrincipal();
56          }
57
58          public Object JavaDoc getCredential()
59          {
60             return SecurityAssociation.getCredential();
61          }
62       };
63
64       SecurityActions PRIVILEGED = new SecurityActions()
65       {
66          private final PrivilegedAction JavaDoc getPrincipalAction = new PrivilegedAction JavaDoc()
67          {
68             public Object JavaDoc run()
69             {
70                return SecurityAssociation.getPrincipal();
71             }
72          };
73
74          private final PrivilegedAction JavaDoc getCredentialAction = new PrivilegedAction JavaDoc()
75          {
76             public Object JavaDoc run()
77             {
78                return SecurityAssociation.getCredential();
79             }
80          };
81
82          public Principal JavaDoc getPrincipal()
83          {
84             return (Principal JavaDoc)AccessController.doPrivileged(getPrincipalAction);
85          }
86
87          public Object JavaDoc getCredential()
88          {
89             return AccessController.doPrivileged(getCredentialAction);
90          }
91       };
92
93       Principal JavaDoc getPrincipal();
94
95       Object JavaDoc getCredential();
96    }
97 }
98
Popular Tags