【问题标题】:Cache Invalidate not working in Shiro缓存无效在 Shiro 中不起作用
【发布时间】:2013-07-17 14:07:15
【问题描述】:

我们正在使用 Apache Shiro 在我们的应用程序中管理身份验证。使用权限时,我使用的是开箱即​​用的基于内存的缓存,这是我的 shiro.ini。

在我的 JSP 中,我使用 shiro:taglib 来检查 hasPerimission 和 lacksPemrissons 标签。如果我更新数据库中的用户角色(因此有相应的权限),并且当用户注销时,他的权限就会更新。 我按预期看到未经授权的页面。但是,仍然会呈现链接,但不应该这样做。这些链接使用以下语法呈现。 (注意:这些链接是常见的header.jsp 的一部分,并包含在所有页面中)

<shiro:hasPermission name="admin:viewPage">
        <a href="/pages/admin.jsp">Admin</a>
</shiro:hasPermission>

我的类JNDIAwareJDBCRealm 继承JdbcRealm,后者继承CacheManagerAwareCacheManagerAware.onLogout() 似乎清除了缓存。

标签库是否在做一些缓存?我是否缺少一些缓存实现?

我的 Shiro.ini 文件:

[main] 
jndiJdbcRealm=com.my.domian.JNDIAwareJDBCRealm
jndiJdbcRealm.jndiDataSourceName=jdbc/mySQLConnection 
jndiJdbcRealm.authenticationQuery = select password FROM users where email_id=?
jndiJdbcRealm.userRolesQuery= select role from user_roles, user where user.email_id =? AND user.id = user_roles.user_id 
jndiJdbcRealm.permissionsQuery= SELECT permission FROM role_permission WHERE role=? jndiJdbcRealm.permissionsLookupEnabled=true

bcryptCredentialsMatcher=com.my.domain.BCryptCredentialsMatcher
jndiJdbcRealm.credentialsMatcher = $bcryptCredentialsMatcher

securityManager.realms = $jndiJdbcRealm builtInCacheManager = org.apache.shiro.cache.MemoryConstrainedCacheManager
securityManager.cacheManager = $builtInCacheManager

# unauthorized page
perms.unauthorizedUrl = /pages/unauthorized.jsp

(意识到 Shiro 的文档非常少。)

【问题讨论】:

    标签: java jsp shiro


    【解决方案1】:

    我让它工作了。注销工作流程在我们的代码中错误地使用了session.invalidate()。所以我需要的两件事是

    1. 通过调用 SecurityUtils.getSubject().logout() 而不是 session.invalidate() 来使用正确的注销工作流程。
    2. 登录时清除缓存如下:

    .

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
         throws AuthenticationException {
        ...
        SimplePrincipalCollection principals = new SimplePrincipalCollection(username, "jndiJdbcRealm");
        super.doClearCache(principals);
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-09
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-28
      相关资源
      最近更新 更多