将请求从一个 servlet 转发到另一个 servlet 是否使其通过定义的阀门?

Does forwarding request from one servlet to another makes it to go through valve defined?

我在 tomcat 中定义了一个验证器阀,所以我有一个未受保护的 servlet(不受安全约束)将预处理这些请求并将其转发到受保护的 servlet。转发将包含必要的参数来验证 valve 定义的用户。将请求从一个 servlet 转发到另一个 servlet 会通过阀门吗?

在此先感谢,我是开发新手,如果我指定的任何条款有误,我深表歉意:)

还有 tomcat 怎么知道这个阀是为验证定义的?

否,所有 Valve 在原始请求期间仅处理一次。

如果你使用RequestDispatcher to transfer the request to another servlet (or JSP page) only the configured Filter的方法之一,就会用到。假定您的 servlet 在转发请求之前执行了必要的安全检查。

要在代码中执行必要的检查,您可以使用如下代码:

// Forces authentication
// If the user was not authenticated, he will need to authenticate
// and resubmit the servlet request.
if (request.authenticate(response)) {
    // Authorization
    if (!request.isUserInRole("admin")) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    // Otherwise forward
    request.getRequestDispatcher("/protected/resource").forward(request, response);
}

您可以在转发 servlet 或适当的 HttpFilter.

中执行这些检查