1 7 package org.jboss.security; 8 9 import java.net.Authenticator ; 10 import java.net.PasswordAuthentication ; 11 import java.security.Principal ; 12 import java.security.AccessController ; 13 import java.security.PrivilegedAction ; 14 15 21 public class SecurityAssociationAuthenticator extends Authenticator  22 { 23 protected PasswordAuthentication getPasswordAuthentication() 24 { 25 SecurityActions sa = SecurityActions.UTIL.getSecurityActions(); 26 Principal principal = sa.getPrincipal(); 27 Object credential = sa.getCredential(); 28 String 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 auth = new PasswordAuthentication (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 getPrincipal() 54 { 55 return SecurityAssociation.getPrincipal(); 56 } 57 58 public Object getCredential() 59 { 60 return SecurityAssociation.getCredential(); 61 } 62 }; 63 64 SecurityActions PRIVILEGED = new SecurityActions() 65 { 66 private final PrivilegedAction getPrincipalAction = new PrivilegedAction () 67 { 68 public Object run() 69 { 70 return SecurityAssociation.getPrincipal(); 71 } 72 }; 73 74 private final PrivilegedAction getCredentialAction = new PrivilegedAction () 75 { 76 public Object run() 77 { 78 return SecurityAssociation.getCredential(); 79 } 80 }; 81 82 public Principal getPrincipal() 83 { 84 return (Principal )AccessController.doPrivileged(getPrincipalAction); 85 } 86 87 public Object getCredential() 88 { 89 return AccessController.doPrivileged(getCredentialAction); 90 } 91 }; 92 93 Principal getPrincipal(); 94 95 Object getCredential(); 96 } 97 } 98 | Popular Tags |