[jboss-cvs] JBossAS SVN: r63024 - projects/security/security-jboss-sx/trunk/src/main/org/jboss/security/plugins.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Sun May 13 12:41:35 EDT 2007


Author: anil.saldhana at jboss.com
Date: 2007-05-13 12:41:35 -0400 (Sun, 13 May 2007)
New Revision: 63024

Modified:
   projects/security/security-jboss-sx/trunk/src/main/org/jboss/security/plugins/JBossAuthenticationManager.java
Log:
authentication cache

Modified: projects/security/security-jboss-sx/trunk/src/main/org/jboss/security/plugins/JBossAuthenticationManager.java
===================================================================
--- projects/security/security-jboss-sx/trunk/src/main/org/jboss/security/plugins/JBossAuthenticationManager.java	2007-05-13 16:41:02 UTC (rev 63023)
+++ projects/security/security-jboss-sx/trunk/src/main/org/jboss/security/plugins/JBossAuthenticationManager.java	2007-05-13 16:41:35 UTC (rev 63024)
@@ -22,6 +22,7 @@
 package org.jboss.security.plugins;
  
 import java.security.Principal;
+import java.util.HashMap;
 import java.util.Map;
 
 import javax.security.auth.Subject;
@@ -29,8 +30,12 @@
 import javax.security.auth.login.LoginContext;
 import javax.security.auth.login.LoginException; 
 
+import org.jboss.logging.Logger;
 import org.jboss.security.AuthenticationManager;
 import org.jboss.security.SecurityConstants;
+import org.jboss.security.cache.JBossAuthenticationCache;
+import org.jboss.security.cache.SecurityCache;
+import org.jboss.security.cache.SecurityCacheException;
 
 //$Id$
 
@@ -42,17 +47,56 @@
  */
 public class JBossAuthenticationManager implements AuthenticationManager
 {
+   private static Logger log = Logger.getLogger(JBossAuthenticationManager.class);
+   
    protected String securityDomain = SecurityConstants.DEFAULT_APPLICATION_POLICY;
    
    protected CallbackHandler callbackHandler = null;
     
    private ThreadLocal<Subject> subjectLocal = new ThreadLocal<Subject>();
+   
+   private SecurityCache<Principal> sCache = null;
+
+   private boolean cacheValidation = false; 
     
    public JBossAuthenticationManager(String sdomain, CallbackHandler cbh)
    {
       this.securityDomain = sdomain;
       this.callbackHandler = cbh;
-   } 
+      sCache = new JBossAuthenticationCache();
+   }
+   
+   /**
+    * Create JBossAuthenticationManager
+    * @param sdomain SecurityDomain
+    * @param cbh CallbackHandler
+    * @param initCapacity Initial Capacity for the internal Security Cache
+    * @param loadFactor Load Factor for the internal Security Cache
+    * @param level Concurrency Level for the internal Security Cach
+    */
+   public JBossAuthenticationManager(String sdomain, CallbackHandler cbh, 
+         int initCapacity, float loadFactor, int level)
+   {
+      this.securityDomain = sdomain;
+      this.callbackHandler = cbh;
+      sCache = new JBossAuthenticationCache(initCapacity, loadFactor, level);
+   }
+   
+   public void setSecurityCache(String className)
+   {
+      if(className == null)
+         throw new IllegalArgumentException("className is null");
+      ClassLoader cl = SubjectActions.getContextClassLoader();
+      try
+      {
+         Class clazz = cl.loadClass(className);
+         sCache = (SecurityCache<Principal>) clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});
+      }
+      catch (Exception e)
+      {
+         throw new RuntimeException(e);
+      }
+   }
 
    /**
     * @see AuthenticationManager#getActiveSubject()
@@ -84,7 +128,7 @@
    public boolean isValid(Principal principal, Object credential)
    {
       return isValid(principal, credential, new Subject());
-   }
+   } 
 
    /**
     * @see AuthenticationManager#isValid(Principal, Object, Subject)
@@ -93,18 +137,74 @@
    {
       if(subject == null)
          throw new IllegalArgumentException("Subject is null"); 
+
+      HashMap<String,Object> map = new HashMap<String,Object>();
+      if(sCache.cacheHit(principal))
+      {
+         Subject cacheSubject = validateCache(principal,credential,subject);
+         if(cacheSubject != null)
+         {
+            subject = cacheSubject;
+            subjectLocal.set(cacheSubject);
+            return true;
+         } 
+      }
       LoginContext lc = null;
       
       try
       {
+         this.cacheValidation = false;
          lc = new LoginContext(securityDomain, subject, callbackHandler);
-         lc.login();
+         lc.login(); 
+
+         map.put(SecurityConstants.CREDENTIAL, credential);
+         map.put(SecurityConstants.SUBJECT, subject);
+         try
+         {
+            sCache.addCacheEntry(principal, map);
+         }
+         catch (SecurityCacheException e)
+         {
+            throw new RuntimeException(e);
+         }
          subjectLocal.set(lc.getSubject());
       }
       catch (LoginException e)
       {
-         throw new RuntimeException(e);
+         log.trace("Login Failure:",e);
+         return false;
       }
       return true;
-   }  
+   } 
+   
+   /**
+    * Value added method for testing alone
+    * @return
+    */
+   public boolean fromCache()
+   {
+      return cacheValidation ;
+   }
+   
+   private Subject validateCache(Principal principal, Object credential, Subject subject)
+   {
+      this.cacheValidation = false;
+      HashMap<String,Object> map = new HashMap<String,Object>();
+      map.put(SecurityConstants.CREDENTIAL, credential);
+      try
+      {
+         sCache.cacheOperation(principal, map);
+         Object cacheReturn = sCache.get(principal); 
+         if(cacheReturn != null && cacheReturn instanceof Subject)
+         {
+            subject = (Subject) cacheReturn;
+            this.cacheValidation = true;
+            return subject; 
+         } 
+      }
+      catch (SecurityCacheException e)
+      { 
+      }
+      return null;
+   }
 }




More information about the jboss-cvs-commits mailing list