确定 JSF 请求是否是用户发起的并忽略静态内容请求

Determine if JSF request is user initiated and ignore static content requests

我想记录所有用户 JSF 请求(包括 AJAX),为此我为 @RequestScoped

实现了一个 @Observer

这有效,但它也会触发所有静态内容 GET 请求,包括 CSS/JS/Images 等我不想记录的请求。

有没有一种方便的方法来确定请求是否由用户发起并忽略静态文件 GET 的?我想检查请求的文件扩展名,但一些静态文件似乎以 .xhtml 结尾,所以这行不通

如果有帮助,我正在使用 Omnifaces (JSF 2.3 / CDI)

public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
    // logging request here
}

前提是您通过 therefor provided JSF components such as <h:outputScript>, <h:outputStylesheet> and <h:graphicImage>, then simply check if the request path starts with ResourceHandler.RESOURCE_IDENTIFIER.

处理 所有 资源
public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
    HttpServletRequest request = (HttpServletRequest) payload;

    if (request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER + "/")) {
        return; // It's a JSF resource request.
    }

    // Log request here.
}

I am using Omnifaces if that helps (JSF 2.3 / CDI)

是的,这绝对有帮助。自 OmniFaces 2.0 版以来,已经有一个辅助方法可以做到这一点,从而减少了样板文件。这是 Servlets#isFacesResourceRequest():

public void logRequest(@Observes @Initialized(RequestScoped.class) ServletRequest payload) {
    HttpServletRequest request = (HttpServletRequest) payload;

    if (Servlets.isFacesResourceRequest(request)) {
        return; // It's a JSF resource request.
    }

    // Log request here.
}