User: jules_gosnell
Date: 02/01/13 05:28:49
Added: jetty/src/main/org/jboss/jetty/security JBossUserRealm.java
Log:
split into dirs
Revision Changes Path
1.1
contrib/jetty/src/main/org/jboss/jetty/security/JBossUserRealm.java
Index: JBossUserRealm.java
===================================================================
/*
* jBoss, the OpenSource EJB server
*
* Distributable under GPL license.
* See terms of license at gnu.org.
*/
// $Id: JBossUserRealm.java,v 1.1 2002/01/13 13:28:49 jules_gosnell Exp $
package org.jboss.jetty.security;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.security.auth.Subject;
import org.jboss.logging.Logger;
import org.jboss.security.AuthenticationManager;
import org.jboss.security.RealmMapping;
import org.jboss.security.SecurityAssociation;
import org.jboss.security.SimplePrincipal;
import org.jboss.security.SubjectSecurityManager;
import org.mortbay.http.HttpRequest;
import org.mortbay.http.UserPrincipal;
import org.mortbay.http.UserRealm;
/** An implementation of UserRealm that integrates with the JBossSX
* security manager associted with the web application.
* @author [EMAIL PROTECTED]
* @version $Revision: 1.1 $
*/
// TODO
public class JBossUserRealm
implements UserRealm // Jetty API
{
class JBossUserPrincipal
extends SimplePrincipal // JBoss API
implements UserPrincipal // Jetty API
{
JBossUserPrincipal(String name)
{
super(name);
_log.info("created JBossUserRealm::JBossUserPrincipal: "+name);
}
protected boolean
isAuthenticated(String password)
{
String userName = this.getName(); // needs disambiguation because our
outer class....
boolean authenticated = false;
if (password==null)
password="";
char[] passwordChars = password.toCharArray();
_log.info("authenticating: Name:"+userName+" Password:"+password);
if(_authMgr!=null &&_authMgr.isValid(this, passwordChars))
{
_log.info("JBossUserPrincipal: "+userName+" is authenticated");
SecurityAssociation.setPrincipal(this);
SecurityAssociation.setCredential(passwordChars);
authenticated=true;
}
else
{
_log.warn("JBossUserPrincipal: "+userName+" is NOT authenticated");
}
return authenticated;
}
public boolean
equals(Object o)
{
if (o==this)
return true;
if (o==null)
return false;
if (getClass()!=o.getClass())
return false;
String myName =this.getName();
String yourName=((JBossUserPrincipal)o).getName();
if (myName==null && yourName==null)
return true;
if (myName!=null && myName.equals(yourName))
return true;
return false;
}
//----------------------------------------
// SimplePrincipal - for JBoss
//----------------------------------------
// UserPrincipal - for Jetty
public boolean
authenticate(String password, HttpRequest request)
{
_password=password;
boolean authenticated=false;
authenticated=isAuthenticated(_password);
// This doesn't mean anything to Jetty - but may to some
// Servlets - confirm later...
if (authenticated && _subjSecMgr!=null)
{
Subject subject = _subjSecMgr.getActiveSubject();
request.setAttribute(_subjAttrName, subject);
}
return authenticated;
}
public boolean
isAuthenticated()
{
return isAuthenticated(_password);
}
public UserRealm
getUserRealm()
{
return JBossUserRealm.this;
}
public boolean
isUserInRole(String role)
{
boolean isUserInRole = false;
String userName = this.getName();
Set requiredRoles = Collections.singleton(new SimplePrincipal(role));
if(_realmMapping!=null && _realmMapping.doesUserHaveRole(this, requiredRoles))
{
_log.info("JBossUserPrincipal: "+userName+" is in Role: "+role);
isUserInRole = true;
}
else
{
_log.warn("JBossUserPrincipal: "+userName+" is NOT in Role: "+role);
}
return isUserInRole;
}
}
private Logger _log;
private String _realmName;
private AuthenticationManager _authMgr;
private RealmMapping _realmMapping;
private HashMap _users = new HashMap();
private String _subjAttrName = "j_subject"; // needs accessors -
TODO
private SubjectSecurityManager _subjSecMgr;
private String _password;
public
JBossUserRealm(String realmName)
{
_realmName = realmName;
_log = Logger.getLogger(JBossUserRealm.class.getName() + "#" + _realmName);
try
{
// can I get away with just doing this lookup once per webapp ?
InitialContext iniCtx = new InitialContext();
// do we need the 'java:comp/env' prefix ? TODO
Context securityCtx =(Context) iniCtx.lookup("java:comp/env/security");
_authMgr =(AuthenticationManager) securityCtx.lookup("securityMgr");
_realmMapping =(RealmMapping) securityCtx.lookup("realmMapping");
iniCtx=null;
if (_authMgr instanceof SubjectSecurityManager)
_subjSecMgr = (SubjectSecurityManager) _authMgr;
}
catch (NamingException e)
{
_log.error("java:comp/env/security does not appear to be correctly set up", e);
}
}
// this is going to cause contention - TODO
private synchronized JBossUserPrincipal
ensureUser(String userName)
{
JBossUserPrincipal user = (JBossUserPrincipal)_users.get(userName);
if (user==null)
{
user=new JBossUserPrincipal(userName);
_users.put(userName, user);
}
return user;
}
public UserPrincipal
getUser(String userName)
{
_log.info("JBossUserPrincipal: "+userName);
return ensureUser(userName);
}
public String
getName()
{
return _realmName;
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development