User: jules_gosnell
Date: 01/10/30 14:45:12
Modified: jetty/src/main/org/jboss/jetty Tag: Branch_2_4
JBossUserRealm.java SetupHandler.java
Log:
changes to security code
release 2.4.3/3.1.3
Revision Changes Path
No revision
No revision
1.2.2.3 +108 -76 contrib/jetty/src/main/org/jboss/jetty/JBossUserRealm.java
Index: JBossUserRealm.java
===================================================================
RCS file: /cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/JBossUserRealm.java,v
retrieving revision 1.2.2.2
retrieving revision 1.2.2.3
diff -u -r1.2.2.2 -r1.2.2.3
--- JBossUserRealm.java 2001/09/17 23:10:57 1.2.2.2
+++ JBossUserRealm.java 2001/10/30 22:45:12 1.2.2.3
@@ -5,11 +5,13 @@
* See terms of license at gnu.org.
*/
-// $Id: JBossUserRealm.java,v 1.2.2.2 2001/09/17 23:10:57 jules_gosnell Exp $
+// $Id: JBossUserRealm.java,v 1.2.2.3 2001/10/30 22:45:12 jules_gosnell Exp $
package org.jboss.jetty;
-import java.util.HashSet;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Set;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Category;
@@ -17,7 +19,6 @@
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;
@@ -25,106 +26,95 @@
/** An implementation of UserRealm that integrates with the JBossSX
* security manager associted with the web application.
* @author [EMAIL PROTECTED]
- * @version $Revision: 1.2.2.2 $
+ * @version $Revision: 1.2.2.3 $
*/
+
public class JBossUserRealm
implements UserRealm
{
- private String _realmName;
- private Category _log;
-
- //----------------------------------------
+ private Category _log;
+ private String _realmName;
+ private EJBSecurityManager _securityMgr;
+ private RealmMapping _realmMapping;
+ private HashMap _users = new HashMap();
class User
extends SimplePrincipal
implements UserPrincipal
{
- HttpRequest _request;
-
- User(String name, HttpRequest request)
+ User(String name)
{
super(name);
- _request = request;
+ _log.info("Security- created JBossUserRealm::User: "+name);
}
-
+
public boolean
- authenticate(String password)
+ equals(Object o)
{
- boolean authenticated = false;
- String username = this.getName(); // needs disambiguation
+ if (o==this)
+ return true;
+
+ if (o==null)
+ return false;
+
+ if (getClass()!=o.getClass())
+ return false;
+
+ String myName =this.getName();
+ String yourName=((User)o).getName();
- _log.info("Authenticating access, username: " + username/* + " " +
_request*/);
+ if (myName==null && yourName==null)
+ return true;
- try
- {
- // Get the JBoss security manager from the ENC context
- InitialContext iniCtx = new InitialContext();
- EJBSecurityManager securityMgr = (EJBSecurityManager)
iniCtx.lookup("java:comp/env/security/securityMgr");
+ if (myName!=null && myName.equals(yourName))
+ return true;
+
+ return false;
+ }
- if(securityMgr.isValid(this, password))
- {
- authenticated = true;
- _request.setAttribute(UserPrincipal.__ATTR, this);
-
- _log.info("User: "+username+" is authenticated");
-
- SecurityAssociation.setPrincipal(this);
- SecurityAssociation.setCredential(password.toCharArray());
- /*
- if( useJAAS == true && securityMgr instanceof SubjectSecurityManager )
- {
- SubjectSecurityManager subjectMgr = (SubjectSecurityManager) securityMgr;
- Subject subject = subjectMgr.getActiveSubject();
- _request.setAttribute(subjectAttributeName, subject);
- }
- */
- }
- else
- {
- _log.info("User: "+username+" is NOT authenticated");
- }
+ public boolean
+ authenticate(String password, HttpRequest request)
+ {
+ boolean authenticated = false;
+ String userName = this.getName(); // needs disambiguation
+
+ // Get the JBoss security manager from the ENC context
+ if(_securityMgr!=null &&_securityMgr.isValid(this, password))
+ {
+ authenticated = true;
+ _log.info("Security- User: "+userName+" is authenticated");
+ SecurityAssociation.setPrincipal(this);
+ SecurityAssociation.setCredential(password.toCharArray());
}
- catch(NamingException e)
+ else
{
- _log.error("Error during authenticate", e);
+ _log.warn("Security- User: "+userName+" is NOT authenticated");
}
-
+
return authenticated;
}
-
+
public boolean
isUserInRole(String role)
{
boolean isUserInRole = false;
- String username = this.getName();
+ String userName = this.getName();
- _log.info("Authorizing access, username: " + username/* + " " + _request*/);
-
- try
+ // Get the JBoss security manager from the ENC context
+ Set requiredRoles = Collections.singleton(role);
+ if(_realmMapping.doesUserHaveRole(this, requiredRoles))
{
- // Get the JBoss security manager from the ENC context
- InitialContext iniCtx = new InitialContext();
- RealmMapping securityMgr = (RealmMapping)
iniCtx.lookup("java:comp/env/security/realmMapping");
- HashSet requiredRoles = new HashSet();
-
- requiredRoles.add(new SimplePrincipal(role));
- if(securityMgr.doesUserHaveRole(this, requiredRoles))
- {
- isUserInRole = true;
- _log.info("User: "+username+" is authorized");
- }
- else
- {
- _log.warn("User: "+username+" is NOT authorized,
requiredRoles="+requiredRoles);
- }
+ isUserInRole = true;
+ _log.info("Security- User: "+userName+" is in Role: "+role);
}
- catch(NamingException e)
+ else
{
- _log.error("Error during authorization", e);
+ _log.warn("Security- User: "+userName+" is NOT in Role: "+role);
}
+
return isUserInRole;
}
-
+
public UserRealm
getUserRealm()
{
@@ -132,14 +122,12 @@
}
}
- //----------------------------------------
-
public
JBossUserRealm(Category log, String realmName)
{
_log = log;
_realmName = realmName;
- _log.info("+++ Created JBossUserRealm, realmName="+_realmName);
+ _log.info("Security- created JBossUserRealm: "+_realmName);
}
public String
@@ -148,10 +136,54 @@
return _realmName;
}
+ /**
+ * @deprecated
+ */
public UserPrincipal
- getUser(String username, HttpRequest request)
+ getUser(String userName, HttpRequest request)
{
- _log.info("+++ JBossUserRealm.getUser, username="+username);
- return new User(username, request);
+ return getUser(userName);
}
+
+ //----------------------------------------
+
+ private synchronized User
+ ensureUser(String userName)
+ {
+ User user = (User)_users.get(userName);
+
+ if (user==null)
+ {
+ user=new User(userName);
+ _users.put(userName, user);
+ }
+
+ return user;
+ }
+
+ public UserPrincipal
+ getUser(String userName)
+ {
+ if (_securityMgr==null)
+ {
+ try
+ {
+ // do we really have to do this every time - is it expensive ? TODO
+ InitialContext iniCtx = new InitialContext();
+ // do we need the 'java:comp/env' prefix ? TODO
+ _securityMgr = (EJBSecurityManager)
iniCtx.lookup("java:comp/env/security/securityMgr");
+ _realmMapping = (RealmMapping)
iniCtx.lookup("java:comp/env/security/realmMapping");
+ }
+ catch (NamingException e)
+ {
+ _log.error("Security- Could not create initial Context", e);
+ }
+ }
+
+ _log.info("Security- User: "+userName);
+
+ return ensureUser(userName);
+ }
+
+ //----------------------------------------
}
1.4.2.4 +9 -5 contrib/jetty/src/main/org/jboss/jetty/SetupHandler.java
Index: SetupHandler.java
===================================================================
RCS file: /cvsroot/jboss/contrib/jetty/src/main/org/jboss/jetty/SetupHandler.java,v
retrieving revision 1.4.2.3
retrieving revision 1.4.2.4
diff -u -r1.4.2.3 -r1.4.2.4
--- SetupHandler.java 2001/10/03 21:55:53 1.4.2.3
+++ SetupHandler.java 2001/10/30 22:45:12 1.4.2.4
@@ -5,7 +5,7 @@
* See terms of license at gnu.org.
*/
-// $Id: SetupHandler.java,v 1.4.2.3 2001/10/03 21:55:53 jules_gosnell Exp $
+// $Id: SetupHandler.java,v 1.4.2.4 2001/10/30 22:45:12 jules_gosnell Exp $
package org.jboss.jetty;
@@ -13,6 +13,7 @@
import org.apache.log4j.Category;
import org.jboss.web.AbstractWebContainer.WebDescriptorParser;
import org.jboss.web.WebApplication;
+import org.mortbay.http.HandlerContext;
import org.mortbay.http.HttpException;
import org.mortbay.http.HttpRequest;
import org.mortbay.http.HttpResponse;
@@ -25,7 +26,7 @@
* so that is sees the correct class loader and security realm name.
*
* @author [EMAIL PROTECTED]
- * @version $Revision: 1.4.2.3 $
+ * @version $Revision: 1.4.2.4 $
*/
public class SetupHandler
extends NullHandler
@@ -72,9 +73,12 @@
_descriptorParser.parseWebAppDescriptors(loader, webAppDD, jbossDD);
// Add the JBoss security realm
- HttpServer server = getHandlerContext().getHttpServer();
- String realmName = getHandlerContext().getRealm();
- server.addRealm(new JBossUserRealm(_log, realmName));
+ HandlerContext context = getHandlerContext();
+ HttpServer server = context.getHttpServer();
+ String realmName = context.getRealm();
+
+ if (realmName!=null)
+ context.setRealm(realmName,new JBossUserRealm(_log, realmName));
super.start();
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development