Jsf 使用ApacheShiro从会话/authenticationtoken/cache检索用户信息

Jsf 使用ApacheShiro从会话/authenticationtoken/cache检索用户信息,jsf,jsf-2,shiro,Jsf,Jsf 2,Shiro,我正在尝试使用JPA数据源与ApacheShiro构建一个动态许可/权限 最后,一切都回到了这个类: public class CustomAuthorizationFilter extends HttpMethodPermissionFilter { @Override public boolean isAccessAllowed(ServletRequest r, ServletResponse r, Object m) throws IOException { //Somehow

我正在尝试使用JPA数据源与ApacheShiro构建一个动态许可/权限

最后,一切都回到了这个类:

public class CustomAuthorizationFilter extends HttpMethodPermissionFilter {

 @Override
 public boolean isAccessAllowed(ServletRequest r, ServletResponse r, Object m) throws IOException {
  //Somehow get the user stored somewhere in the servlet memory
  SysUser loggedUser = %some_code_here%
  for (String allowance : loggedUser.getAllowances()) {
   // Do many validations
   if (pathsMatch(allowance, request)) {
    return true;
   }
  }

  return super.isAccessAllowed(request, response, mappedValue);
 }

}
isAccessAllowed()方法在每次请求时都被激发,因此我不想从数据库中获取信息。Shiro构建了许多关于用户的对象,其中一个是AuthorizationInfo。我构建了一个CustomAuthorizationInfo,其中权限和津贴列表位于。。。但是如何在不重新访问数据库的情况下检索这些数据呢

是否可以在不访问数据库的情况下使用shiro存储/检索授权用户的信息


(注:像isPermitt这样的方法不能解决问题,因为我需要使用pathsMatch()方法的权限本身)。

Shiro内置了一个缓存机制,这样当用户登录时,所有的构建凭据都存储在缓存中,只要用户没有注销,每次后续调用都会在缓存中检索

如果您签出authorizationrealm.getAuthorizationInfo的源,可以看到它在配置缓存时从缓存中检索AuthorizationInfo

请在此处查看有关缓存的文档:

快速解决方案是配置一个简单的内存缓存:

cacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $cacheManager
这将导致每个用户会话仅执行一个数据库操作来检索凭据