ozone.security.session.ClusteredSessionRegistryImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ozone-security Show documentation
Show all versions of ozone-security Show documentation
Security plugin base classes (and sample support classes) for
OWF and Marketplace
The newest version!
package ozone.security.session;
/**
* This class is based on the example code posted at
* http://scalejava.blogspot.com/2012/12/clustered-spring-sessionregistry.html.
*
* It implements the spring SessionRegistry in a way that can be clustered
* using ehcache
*/
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpSession;
import net.sf.ehcache.Cache;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import net.sf.ehcache.event.CacheEventListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.security.core.session.SessionDestroyedEvent;
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.HttpSessionDestroyedEvent;
import org.springframework.util.Assert;
import ozone.security.CacheManagerFactory;
public class ClusteredSessionRegistryImpl implements SessionRegistry, ApplicationListener {
protected final Log logger = LogFactory.getLog(ClusteredSessionRegistryImpl.class);
private static final String CACHE_NAME = "sessionIds";
private CacheManager cacheManager;
private Cache sessionIds;
/**
* Implements the event listening needed to reattach a ClusteredSessionInformation
* to this SessionRegistry.
*
* Unfortunately, the ehcache API requires us to implement a bunch of extra functions
* for listening to other events that we aren't interested in
*/
private class CacheListener implements CacheEventListener {
public Object clone() { return this.clone(); }
public void dispose() {}
public void notifyElementEvicted(Ehcache c, Element e) {}
public void notifyElementExpired(Ehcache c, Element e) {}
public void notifyElementRemoved(Ehcache c, Element e) {}
public void notifyRemoveAll(Ehcache c) {}
/**
* Set the sessionregistry on the newly added element. For elements created
* by this node, this should have no effect. For element received from other
* cluster nodes, this sets the registry to allow changes to be sent back
*/
private void setSessionRegistry(Element element) {
ClusteredSessionInformation info = (ClusteredSessionInformation)element.getValue();
if (!info.hasSessionRegistry()) {
info.setSessionRegistry(ClusteredSessionRegistryImpl.this);
logger.debug("setting session registry on element- " + info.getSessionId());
}
}
public void notifyElementPut(Ehcache cache, Element element) {
setSessionRegistry(element);
}
public void notifyElementUpdated(Ehcache c, Element element) {
setSessionRegistry(element);
}
}
public ClusteredSessionRegistryImpl() {
cacheManager = CacheManagerFactory.getCacheManager();
sessionIds = cacheManager.getCache(CACHE_NAME);
sessionIds.getCacheEventNotificationService().registerListener(this.new CacheListener());
}
@Override
public void onApplicationEvent(SessionDestroyedEvent event) {
logger.debug("onApplicationEvent");
if (event instanceof HttpSessionDestroyedEvent) {
String sessionId = ((HttpSession) event.getSource()).getId();
removeSessionInformation(sessionId);
}
}
@Override
public List