Commit 2de3bb62 authored by michael.simon's avatar michael.simon
Browse files

Cache roles in AuthorizationBean to speed up left-side-bar.xhtml

parent 9f3f7477
......@@ -37,6 +37,7 @@ import edu.kit.scc.webreg.service.RegistryService;
import edu.kit.scc.webreg.service.RoleService;
import edu.kit.scc.webreg.service.ServiceService;
import edu.kit.scc.webreg.service.UserService;
import edu.kit.scc.webreg.util.RoleCache;
import edu.kit.scc.webreg.util.SessionManager;
@Named("authorizationBean")
......@@ -73,6 +74,9 @@ public class AuthorizationBean implements Serializable {
@Inject
private ApplicationConfig appConfig;
@Inject
private RoleCache roleCache;
@PostConstruct
private void init() {
if (sessionManager.getUserId() == null)
......@@ -144,12 +148,12 @@ public class AuthorizationBean implements Serializable {
if (roleName.startsWith("ROLE_"))
roleName = roleName.substring(5);
RoleEntity role = roleService.findByName(roleName);
Long roleId = roleCache.getIdFromRolename(roleName);
if (role == null)
if (roleId == null)
return false;
return sessionManager.isUserInRole(role.getId());
return sessionManager.isUserInRole(roleId);
}
public boolean isUserInRole(RoleEntity role) {
......
......@@ -104,16 +104,19 @@ public class Saml2PostHandlerServlet {
String persistentId = saml2AssertionService.extractPersistentId(assertion, spConfig);
logger.debug("Storing relevant SAML data in session");
session.setPersistentId(persistentId);
Map<String, List<Object>> attributeMap = saml2AssertionService.extractAttributes(assertion);
session.setAttributeMap(attributeMap);
UserEntity user = userService.findByPersistentWithRoles(spConfig.getEntityId(),
idpEntity.getEntityId(), persistentId);
if (user == null) {
logger.info("New User detected, sending to register Page");
// Store SAML Data temporarily in Session
logger.debug("Storing relevant SAML data in session");
session.setPersistentId(persistentId);
session.setAttributeMap(attributeMap);
// Role -1 is for new users
session.addRole(-1L);
response.sendRedirect("/register/register.xhtml");
......
package edu.kit.scc.webreg.util;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import org.slf4j.Logger;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
import edu.kit.scc.webreg.entity.RoleEntity;
import edu.kit.scc.webreg.service.RoleService;
@ApplicationScoped
public class RoleCache {
@Inject
private Logger logger;
@Inject
private RoleService roleService;
private LoadingCache<String, Long> cache;
@PostConstruct
public void init() {
logger.info("Initializing roleCache");
cache = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.maximumSize(1000)
.expireAfterWrite(1, TimeUnit.MINUTES)
.removalListener(removalListener)
.build(cacheLoader);
}
public Long getIdFromRolename(String roleName) {
try {
return cache.get(roleName);
} catch (ExecutionException e) {
logger.info("Execution Exception on cache", e);
return null;
}
}
private CacheLoader<String, Long> cacheLoader = new CacheLoader<String, Long>() {
public Long load(String key) {
RoleEntity role = roleService.findByName(key);
if (role != null)
return role.getId();
return null;
}
};
private RemovalListener<String, Long> removalListener = new RemovalListener<String, Long>() {
public void onRemoval(RemovalNotification<String, Long> removal) {
if (logger.isTraceEnabled())
logger.trace("Removing entry {} -> {} from roleCache ({})",
removal.getKey(), removal.getValue(), removal.getCause());
}
};
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment