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;
......@@ -73,6 +74,9 @@ public class AuthorizationBean implements Serializable {
private ApplicationConfig appConfig;
private RoleCache roleCache;
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");
Map<String, List<Object>> attributeMap = saml2AssertionService.extractAttributes(assertion);
UserEntity user = userService.findByPersistentWithRoles(spConfig.getEntityId(),
idpEntity.getEntityId(), persistentId);
if (user == null) {"New User detected, sending to register Page");
// Store SAML Data temporarily in Session
logger.debug("Storing relevant SAML data in session");
// Role -1 is for new users
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 edu.kit.scc.webreg.entity.RoleEntity;
import edu.kit.scc.webreg.service.RoleService;
public class RoleCache {
private Logger logger;
private RoleService roleService;
private LoadingCache<String, Long> cache;
public void init() {"Initializing roleCache");
cache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.MINUTES)
public Long getIdFromRolename(String roleName) {
try {
return cache.get(roleName);
} catch (ExecutionException e) {"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());
Supports Markdown
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