1 23 package com.sun.enterprise.security; 24 25 import java.security.Principal ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import javax.security.auth.Subject ; 29 30 import com.sun.enterprise.security.auth.login.PasswordCredential; 31 import com.sun.enterprise.deployment.PrincipalImpl; 32 import com.sun.enterprise.ServerConfiguration; 33 34 import java.util.logging.*; 35 import com.sun.logging.*; 36 37 38 43 public final class ClientSecurityContext extends AbstractSecurityContext { 44 45 private static Logger _logger = 46 LogDomains.getLogger(LogDomains.SECURITY_LOGGER); 47 48 private static final boolean isPerThreadAuth = 50 Boolean.getBoolean("com.sun.appserv.iiopclient.perthreadauth"); 51 52 private static Object csc = isPerThreadAuth ? new ThreadLocal () : null; 53 54 59 public ClientSecurityContext(String userName, 60 Subject s) { 61 62 this.initiator = new PrincipalImpl(userName); 63 this.subject = s ; 64 } 65 66 70 public static ClientSecurityContext init() { 71 ClientSecurityContext sc = getCurrent(); 72 if (sc == null) { sc = generateDefaultSecurityContext(); 75 } 76 return sc; 77 } 78 79 private static ClientSecurityContext generateDefaultSecurityContext() { 80 final String PRINCIPAL_NAME = "auth.default.principal.name"; 81 final String PRINCIPAL_PASS = "auth.default.principal.password"; 82 83 ServerConfiguration config = ServerConfiguration.getConfiguration(); 84 String username = config.getProperty(PRINCIPAL_NAME, "guest"); 85 String password = config.getProperty(PRINCIPAL_PASS, "guest123"); 86 87 synchronized (ClientSecurityContext.class) { 88 try { 90 final Subject subject = new Subject (); 91 final PasswordCredential pc = new PasswordCredential(username, 92 password, "default"); 93 AccessController.doPrivileged(new PrivilegedAction () { 94 public java.lang.Object run() { 95 subject.getPrivateCredentials().add(pc); 96 return null; 97 } 98 }); 99 ClientSecurityContext defaultCSC = 102 new ClientSecurityContext(username, subject); 103 setCurrent(defaultCSC); 104 return defaultCSC; 105 } catch(Exception e) { 106 _logger.log(Level.SEVERE, 107 "java_security.gen_security_context", e); 108 return null; 109 } 110 } 111 } 112 113 public static void reset(ClientSecurityContext sc) { 114 if (isPerThreadAuth) { 115 ((ThreadLocal )csc).set(sc); 116 } else { 117 csc = sc; 118 } 119 } 120 121 130 public static ClientSecurityContext getCurrent() { 131 if (isPerThreadAuth) { 132 return (ClientSecurityContext)((ThreadLocal )csc).get(); 133 } else { 134 return (ClientSecurityContext)csc; 135 } 136 } 137 138 143 public static void setCurrent(ClientSecurityContext sc) { 144 if (isPerThreadAuth) { 145 ((ThreadLocal )csc).set(sc); 146 } else { 147 csc = sc; 148 } 149 } 150 151 158 public Principal getCallerPrincipal() { 159 return initiator; 160 } 161 162 163 public Subject getSubject() { 164 return subject; 165 } 166 167 public String toString() { 168 return "ClientSecurityContext[ " + "Initiator: " + initiator + 169 "Subject " + subject + " ]"; 170 } 171 172 } 173 174 175 176 177 178 179 180 | Popular Tags |