jsf之使用 PhaseListener 而不是 Servlet Filter 进行授权的限制

mayingbao 阅读:37 2024-09-07 23:24:14 评论:0

我目前正在使用如下所示的 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"还提到了以下内容:

You shouldn't couple authorization that tight with JSF. Better make use of container managed authentication and/or a simple filter acting on an url-pattern covering the protected pages.

我不完全理解在执行用户授权时使用 PhaseListener 而不是 Filter 的局限性。谁能给我解释一下?

请您参考如下方法:

PhaseListener 仅在 JSF 请求(即调用 FacesServlet 的 HTTP 请求)上触发。当执行非 JSF 请求时它不会被触发,因此暴露了非 JSF 请求的潜在安全漏洞。无论目标 servlet 是什么,都可以在每个 HTTP 请求上触发 servlet Filter

换句话说:HTTP 请求授权不应与可用的 FacesContext 相关联,而应与可用的 ServletRequest 相关联。始终尝试授权尽可能“低级别”。


标签:Servlet
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号