使用PhaseListener而不是Servlet过滤器进行授权的限制

时间:2015-04-13 07:48:11

标签: jsf jsf-2 authorization servlet-filters phaselistener

我目前正在使用以下PhaseListener来执行用户授权。

private PhaseId phaseId = PhaseId.RESTORE_VIEW;

@Override
public void afterPhase(PhaseEvent event) {

    FacesContext fc = event.getFacesContext();
    boolean isOnAllowedPage = false;
    String[] allowedPages = choseRightPages(); // chose pages for role

    for (String s : allowedPages) {
        if (fc.getViewRoot().getViewId().lastIndexOf(s) > -1) {
            isOnAllowedPage = true;
            break;
        }
    }

    if (!isOnAllowedPage) {
        NavigationHandler nh = fc.getApplication().getNavigationHandler();
        nh.handleNavigation(fc, null, "prohibited");
    }
}

它符合我的要求,但我没有看到它列在How to handle authentication/authorization with users in a database?中,而this Coderanch topic titled "authorization with phaselistener problem"也提到了以下内容:

  

您不应该将与JSF密切相关的授权联系起来。最好使用容器管理的身份验证和/或作用于覆盖受保护页面的url模式的简单过滤器。

我不完全理解在执行用户授权时使用PhaseListener而不是Filter的限制。有人可以向我解释一下吗?

1 个答案:

答案 0 :(得分:6)

仅在JSF请求(即调用PhaseListener的HTTP请求)上触发FacesServlet。执行非JSF请求时不会触发它,从而暴露非JSF请求的潜在安全漏洞。无论目标servlet如何,都可以在每个HTTP请求上触发servlet Filter

换句话说:HTTP请求授权不应该与FacesContext可用,而是与ServletRequest可用。总是尝试授权为"低级"尽可能。