您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Java可以从<filter-mapping>内部的<url-pattern>中排除一些具体的url吗?

Java可以从<filter-mapping>内部的<url-pattern>中排除一些具体的url吗?

标准Servlet API不支持功能。你可能想要为此使用改写URL过滤器(例如Tuckey的过滤器)(与Apache HTTPD的过滤器非常相似mod_rewrite),或者doFilter()在Filter侦听的方法添加一个检查/*

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    // Do your business stuff here for all paths other than /specialpath.
}

如有必要,可以将要忽略的路径指定为init-param过滤器的,以便web.xml无论如何都可以对其进行控制。你可以按以下方式在过滤器中获取它:

private String pathToBeIgnored;

public void init(FilterConfig config) {
    pathToBeIgnored = config.getInitParameter("pathToBeIgnored");
}

如果过滤器是第三方API的一部分,因此你无法对其进行修改,则将其映射到更具体的url-pattern,例如/otherfilterpath/*,创建一个新过滤器,/*并转发到与第三方过滤器匹配的路径。

String path = ((HttpServletRequest) request).getRequestURI();
if (path.startsWith("/specialpath/")) {
    chain.doFilter(request, response); // Just continue chain.
} else {
    request.getRequestDispatcher("/otherfilterpath" + path).forward(request, response);
}

为避免此过滤器在无限循环中调用自身,你需要让其REQUEST仅在(第三方)过滤器上侦听(调度)FORWARD。

java 2022/1/1 18:24:34 有396人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶