为 servlet 映射指定负路径匹配

发布于 2024-12-20 23:36:51 字数 88 浏览 0 评论 0原文

有没有办法在 web.xml 中指定负映射?例如,我想为除匹配“/public/*”之外的所有请求设置过滤器。

Is there a way to specify negative mappings in web.xml? For example, I want to set a filter for ALL requests EXCEPT those matching '/public/*'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-12-27 23:36:51

不,那不可能。您必须在 doFilter() 方法中自己进行 URL 模式匹配。将过滤器映射到 /* 上并执行以下操作:

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith("/public/")) {
    chain.doFilter(request, response);
    return;
}

// ...

或者当实际存在上下文路径时:

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith(req.getContextPath() + "/public/")) {
    chain.doFilter(request, response);
    return;
}

// ...

No, that's not possible. You'd have to do the URL pattern matching yourself inside the doFilter() method. Map the filter on /* and do the following job:

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith("/public/")) {
    chain.doFilter(request, response);
    return;
}

// ...

or when there's actually a context path:

HttpServletRequest req = (HttpServletRequest) request;

if (req.getRequestURI().startsWith(req.getContextPath() + "/public/")) {
    chain.doFilter(request, response);
    return;
}

// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文