Specify negative path match for servlet mapping

前端 未结 1 1550
情书的邮戳
情书的邮戳 2021-01-17 23:24

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/*\'.

相关标签:
1条回答
  • 2021-01-18 00:03

    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;
    }
    
    // ...
    
    0 讨论(0)
提交回复
热议问题