【问题标题】:Configure Apache Shiro to load [urls] section fully from JPA Entities配置 Apache Shiro 以完全从 JPA 实体加载 [urls] 部分
【发布时间】:2014-04-17 14:04:02
【问题描述】:

我似乎找不到从我的 JPA 对象加载 [users] AND [urls] 的示例。 我只想将 shiro.ini 用于 [main] 部分。

到目前为止,我实现的源代码是这样的: Unable to @Inject my DAO in a Custom Apache Shiro AuthorizingRealm

有没有从数据库完全加载 [users] (user/pass) AND [urls] (roles, permissions) 的示例?我似乎在任何地方都找不到。我现在正在寻找它 1 周。

【问题讨论】:

  • 你在 web.xml 中使用了 org.apache.shiro.web.env.EnvironmentLoaderListener 吗?
  • 我正在使用我自己的扩展 EnvironmentLoaderListener 的 CustomEnvironmentLoaderListener。它在 web.xml 中声明。

标签: jsf jsf-2 shiro


【解决方案1】:

经过长时间的研究,我想出的“最佳”解决方案是:

shiro.ini

[main]
jsfFilter = com.test.security.CustomAuthorizationFilter
jsfFilter.loginUrl = /login.jsf

[urls]
/** = jsfFilter

// 您可以在此过滤器中添加过滤 BalusC 描述的 Ajax 请求的方法。 CustomAuthorizationFilter.java

public class CustomAuthorizationFilter extends AuthorizationFilter {

@Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {

        HttpServletRequest httpRequest = (HttpServletRequest) request;

        if (!httpRequest.getRequestURI().startsWith(httpRequest.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) {

            Subject subject = SecurityUtils.getSubject();

            AuthenticatingSecurityManager authenticatingSecurityManager = ((AuthenticatingSecurityManager) SecurityUtils.getSecurityManager());

            PrincipalCollection principals = subject.getPrincipals();
            JPARealm jpaRealm = (JPARealm) authenticatingSecurityManager.getRealms().iterator().next();
            AuthorizationInfo authorizationInfo = jpaRealm.getAuthorizationInfo(principals);

            for (String permission : authorizationInfo.getStringPermissions()) {
                if (pathsMatch(permission, request)) {
                    return true;
                }
            }

        } else {
            return true;
        }
        return false;
    }
}

pathsMatch(permission, request) 方法将尝试验证/比较接收到的字符串权限与用户尝试访问的路径。 此过滤器依赖于始终拥有经过身份验证的用户。 如果subject.getPrincipal() 为空,则需要更多编码。 如果有人需要完整的代码,请告诉我。

【讨论】:

  • 嗨 MBarni,我也有类似的要求来保存和加载数据库上的 URL 授权,所以请您分享整个代码。所以我可以理解它的实现方式。
猜你喜欢
  • 2014-04-11
  • 2017-01-22
  • 2019-06-29
  • 2016-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-11
  • 2011-01-01
相关资源
最近更新 更多