public Context getInitialContext(Hashtable env) throws NamingException {
// Get the login principal and credentials from the JNDI env
Object credentials = env.get(Context.SECURITY_CREDENTIALS);
Object principal = env.get(Context.SECURITY_PRINCIPAL);
Principal securityPrincipal = null;
/** Flag indicating if the SecurityAssociation existing at login should
be restored on logout.
*/
String flag = (String) env.get("jnp.multi-threaded");
if (Boolean.valueOf(flag).booleanValue() == true)
{
/* Turn on the server mode which uses thread local storage for
the principal information.
*/
SecurityAssociationActions.setServer();
}
boolean restoreLoginIdentity = false;
flag = (String) env.get("jnp.restoreLoginIdentity");
if( flag != null )
restoreLoginIdentity = Boolean.parseBoolean(flag);
// See if the principal is a Principal or String
if( principal instanceof Principal )
{
securityPrincipal = (Principal) principal;
}
else
{
// Simply convert this to a name using toString
String username = principal.toString();
securityPrincipal = new SimplePrincipal(username);
}
// Associate this security context
if( restoreLoginIdentity )
{
SecurityAssociationActions.setPrincipalInfo(securityPrincipal, credentials, null);
}
else
{
SecurityAssociationActions.setPrincipalInfo(securityPrincipal, credentials);
}
// Now return the context using the standard jnp naming context factory
Context iniCtx = super.getInitialContext(env);
if( restoreLoginIdentity )
{
// Use a proxy to pop the stack when the context is closed
ClassLoader loader = SecurityAssociationActions.getContextClassLoader();
ContextProxy handler = new ContextProxy(iniCtx);
Class[] ifaces = {Context.class};
iniCtx = (Context) Proxy.newProxyInstance(loader, ifaces, handler);
}
return iniCtx;
}
Take the env Context.SECURITY_PRINCIPAL and Context.SECURITY_CREDENTIALS
and propagate these to the SecurityAssociation principal and credential.
If Context.SECURITY_PRINCIPAL is a java.security.Principal then it is
used as is, otherwise its treated as a name using toString and a
SimplePrincipal is created. The Context.SECURITY_CREDENTIALS is passed
as is. |