mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 01:15:44 +01:00
cache authentication for 60 seconds to improve svn and bzr performance
This commit is contained in:
@@ -186,8 +186,35 @@ public class AuthenticationResult
|
|||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean isCacheable()
|
||||||
|
{
|
||||||
|
return cacheable;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- set methods ----------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param cacheable
|
||||||
|
*/
|
||||||
|
public void setCacheable(boolean cacheable)
|
||||||
|
{
|
||||||
|
this.cacheable = cacheable;
|
||||||
|
}
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private boolean cacheable = true;
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private Collection<String> groups;
|
private Collection<String> groups;
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
|
import sonia.scm.cache.CacheManager;
|
||||||
|
import sonia.scm.cache.SimpleCache;
|
||||||
|
import sonia.scm.security.EncryptionHandler;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
import sonia.scm.util.AssertUtil;
|
import sonia.scm.util.AssertUtil;
|
||||||
import sonia.scm.util.IOUtil;
|
import sonia.scm.util.IOUtil;
|
||||||
@@ -63,6 +66,9 @@ import javax.servlet.http.HttpServletResponse;
|
|||||||
public class ChainAuthenticatonManager extends AbstractAuthenticationManager
|
public class ChainAuthenticatonManager extends AbstractAuthenticationManager
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
public static final String CACHE_NAME = "sonia.cache.auth";
|
||||||
|
|
||||||
/** the logger for ChainAuthenticatonManager */
|
/** the logger for ChainAuthenticatonManager */
|
||||||
private static final Logger logger =
|
private static final Logger logger =
|
||||||
LoggerFactory.getLogger(ChainAuthenticatonManager.class);
|
LoggerFactory.getLogger(ChainAuthenticatonManager.class);
|
||||||
@@ -74,13 +80,20 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param authenticationHandlerSet
|
* @param authenticationHandlerSet
|
||||||
|
* @param encryptionHandler
|
||||||
|
* @param cacheManager
|
||||||
*/
|
*/
|
||||||
@Inject
|
@Inject
|
||||||
public ChainAuthenticatonManager(
|
public ChainAuthenticatonManager(
|
||||||
Set<AuthenticationHandler> authenticationHandlerSet)
|
Set<AuthenticationHandler> authenticationHandlerSet,
|
||||||
|
EncryptionHandler encryptionHandler, CacheManager cacheManager)
|
||||||
{
|
{
|
||||||
AssertUtil.assertIsNotEmpty(authenticationHandlerSet);
|
AssertUtil.assertIsNotEmpty(authenticationHandlerSet);
|
||||||
|
AssertUtil.assertIsNotNull(cacheManager);
|
||||||
this.authenticationHandlerSet = authenticationHandlerSet;
|
this.authenticationHandlerSet = authenticationHandlerSet;
|
||||||
|
this.encryptionHandler = encryptionHandler;
|
||||||
|
this.cache = cacheManager.getSimpleCache(String.class,
|
||||||
|
AuthenticationCacheValue.class, CACHE_NAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
//~--- methods --------------------------------------------------------------
|
||||||
@@ -99,6 +112,74 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
|
|||||||
@Override
|
@Override
|
||||||
public AuthenticationResult authenticate(HttpServletRequest request,
|
public AuthenticationResult authenticate(HttpServletRequest request,
|
||||||
HttpServletResponse response, String username, String password)
|
HttpServletResponse response, String username, String password)
|
||||||
|
{
|
||||||
|
AssertUtil.assertIsNotEmpty(username);
|
||||||
|
AssertUtil.assertIsNotEmpty(password);
|
||||||
|
|
||||||
|
String encryptedPassword = encryptionHandler.encrypt(password);
|
||||||
|
AuthenticationResult ar = getCached(username, encryptedPassword);
|
||||||
|
|
||||||
|
if (ar == null)
|
||||||
|
{
|
||||||
|
ar = doAuthentication(request, response, username, password);
|
||||||
|
|
||||||
|
if ((ar != null) && ar.isCacheable())
|
||||||
|
{
|
||||||
|
cache.put(username,
|
||||||
|
new AuthenticationCacheValue(ar, encryptedPassword));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (logger.isDebugEnabled())
|
||||||
|
{
|
||||||
|
logger.debug("authenticate {} via cache", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException
|
||||||
|
{
|
||||||
|
for (AuthenticationHandler authenticator : authenticationHandlerSet)
|
||||||
|
{
|
||||||
|
IOUtil.close(authenticator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param context
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void init(SCMContextProvider context)
|
||||||
|
{
|
||||||
|
for (AuthenticationHandler authenticator : authenticationHandlerSet)
|
||||||
|
{
|
||||||
|
authenticator.init(context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param request
|
||||||
|
* @param response
|
||||||
|
* @param username
|
||||||
|
* @param password
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private AuthenticationResult doAuthentication(HttpServletRequest request,
|
||||||
|
HttpServletResponse response, String username, String password)
|
||||||
{
|
{
|
||||||
AuthenticationResult ar = null;
|
AuthenticationResult ar = null;
|
||||||
|
|
||||||
@@ -142,38 +223,80 @@ public class ChainAuthenticatonManager extends AbstractAuthenticationManager
|
|||||||
return ar;
|
return ar;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
//~--- get methods ----------------------------------------------------------
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void close() throws IOException
|
|
||||||
{
|
|
||||||
for (AuthenticationHandler authenticator : authenticationHandlerSet)
|
|
||||||
{
|
|
||||||
IOUtil.close(authenticator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method description
|
* Method description
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* @param context
|
* @param username
|
||||||
|
* @param encryptedPassword
|
||||||
|
*
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
@Override
|
private AuthenticationResult getCached(String username,
|
||||||
public void init(SCMContextProvider context)
|
String encryptedPassword)
|
||||||
{
|
{
|
||||||
for (AuthenticationHandler authenticator : authenticationHandlerSet)
|
AuthenticationResult result = null;
|
||||||
|
AuthenticationCacheValue value = cache.get(username);
|
||||||
|
|
||||||
|
if (value != null)
|
||||||
{
|
{
|
||||||
authenticator.init(context);
|
String cachedPassword = value.password;
|
||||||
|
|
||||||
|
if (cachedPassword.equals(encryptedPassword))
|
||||||
|
{
|
||||||
|
result = value.authenticationResult;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- inner classes --------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class description
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @version Enter version here..., 2011-01-15
|
||||||
|
* @author Sebastian Sdorra
|
||||||
|
*/
|
||||||
|
private static class AuthenticationCacheValue
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs ...
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param authenticationResult
|
||||||
|
* @param password
|
||||||
|
*/
|
||||||
|
public AuthenticationCacheValue(AuthenticationResult authenticationResult,
|
||||||
|
String password)
|
||||||
|
{
|
||||||
|
this.authenticationResult = authenticationResult;
|
||||||
|
this.password = password;
|
||||||
|
}
|
||||||
|
|
||||||
|
//~--- fields -------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private AuthenticationResult authenticationResult;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private Set<AuthenticationHandler> authenticationHandlerSet;
|
private Set<AuthenticationHandler> authenticationHandlerSet;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private SimpleCache<String, AuthenticationCacheValue> cache;
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private EncryptionHandler encryptionHandler;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,16 @@
|
|||||||
diskExpiryThreadIntervalSeconds="2400"
|
diskExpiryThreadIntervalSeconds="2400"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
<cache
|
||||||
|
name="sonia.cache.auth"
|
||||||
|
maxElementsInMemory="200000"
|
||||||
|
eternal="false"
|
||||||
|
overflowToDisk="false"
|
||||||
|
timeToIdleSeconds="30"
|
||||||
|
timeToLiveSeconds="60"
|
||||||
|
diskPersistent="false"
|
||||||
|
/>
|
||||||
|
|
||||||
<cache
|
<cache
|
||||||
name="sonia.cache.repository"
|
name="sonia.cache.repository"
|
||||||
maxElementsInMemory="20000"
|
maxElementsInMemory="20000"
|
||||||
|
|||||||
@@ -39,6 +39,9 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import sonia.scm.AbstractTestBase;
|
import sonia.scm.AbstractTestBase;
|
||||||
import sonia.scm.SCMContextProvider;
|
import sonia.scm.SCMContextProvider;
|
||||||
|
import sonia.scm.cache.CacheManager;
|
||||||
|
import sonia.scm.cache.EhCacheManager;
|
||||||
|
import sonia.scm.security.MessageDigestEncryptionHandler;
|
||||||
import sonia.scm.user.User;
|
import sonia.scm.user.User;
|
||||||
import sonia.scm.user.UserTestData;
|
import sonia.scm.user.UserTestData;
|
||||||
import sonia.scm.util.MockUtil;
|
import sonia.scm.util.MockUtil;
|
||||||
@@ -69,8 +72,8 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
|
|||||||
@Test
|
@Test
|
||||||
public void testAuthenticateFailed()
|
public void testAuthenticateFailed()
|
||||||
{
|
{
|
||||||
AuthenticationResult result = manager.authenticate(request, response, trillian.getName(),
|
AuthenticationResult result = manager.authenticate(request, response,
|
||||||
"trillian");
|
trillian.getName(), "trillian");
|
||||||
|
|
||||||
assertNull(result);
|
assertNull(result);
|
||||||
}
|
}
|
||||||
@@ -82,7 +85,8 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
|
|||||||
@Test
|
@Test
|
||||||
public void testAuthenticateNotFound()
|
public void testAuthenticateNotFound()
|
||||||
{
|
{
|
||||||
AuthenticationResult result = manager.authenticate(request, response, "dent", "trillian");
|
AuthenticationResult result = manager.authenticate(request, response,
|
||||||
|
"dent", "trillian");
|
||||||
|
|
||||||
assertNull(result);
|
assertNull(result);
|
||||||
}
|
}
|
||||||
@@ -94,8 +98,8 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
|
|||||||
@Test
|
@Test
|
||||||
public void testAuthenticateSuccess()
|
public void testAuthenticateSuccess()
|
||||||
{
|
{
|
||||||
AuthenticationResult result = manager.authenticate(request, response, trillian.getName(),
|
AuthenticationResult result = manager.authenticate(request, response,
|
||||||
"trillian123");
|
trillian.getName(), "trillian123");
|
||||||
|
|
||||||
assertNotNull(result);
|
assertNotNull(result);
|
||||||
assertUserEquals(trillian, result.getUser());
|
assertUserEquals(trillian, result.getUser());
|
||||||
@@ -126,7 +130,8 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
|
|||||||
trillian.setPassword("trillian123");
|
trillian.setPassword("trillian123");
|
||||||
handlerSet.add(new SingleUserAuthenticaionHandler("trilliansType",
|
handlerSet.add(new SingleUserAuthenticaionHandler("trilliansType",
|
||||||
trillian));
|
trillian));
|
||||||
manager = new ChainAuthenticatonManager(handlerSet);
|
manager = new ChainAuthenticatonManager(handlerSet,
|
||||||
|
new MessageDigestEncryptionHandler(), cacheManager);
|
||||||
manager.init(contextProvider);
|
manager.init(contextProvider);
|
||||||
request = MockUtil.getHttpServletRequest();
|
request = MockUtil.getHttpServletRequest();
|
||||||
response = MockUtil.getHttpServletResponse();
|
response = MockUtil.getHttpServletResponse();
|
||||||
@@ -266,6 +271,9 @@ public class ChainAuthenticationManagerTest extends AbstractTestBase
|
|||||||
|
|
||||||
//~--- fields ---------------------------------------------------------------
|
//~--- fields ---------------------------------------------------------------
|
||||||
|
|
||||||
|
/** Field description */
|
||||||
|
private CacheManager cacheManager = new EhCacheManager();
|
||||||
|
|
||||||
/** Field description */
|
/** Field description */
|
||||||
private ChainAuthenticatonManager manager;
|
private ChainAuthenticatonManager manager;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user