如何使用filter更改请求uri并将其传递给下一个filter?

xcitsw88  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(1016)

我想更改请求uri /aaa/bbb/ccc/bbb/ccc . 换句话说,我想删除请求uri的第一部分。
我试着使用下面的过滤器,但它不工作。

  1. public class AnyRequestHandlerFilter implements Filter, Ordered {
  2. @Override
  3. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  4. throws IOException, ServletException {
  5. HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
  6. System.out.println(httpRequest.getRequestURI());
  7. String path = httpRequest.getRequestURI();
  8. if (path.contains("/aaa")) {
  9. log.info("front:{}", path);
  10. path = path.replace("/aaa/", "/");
  11. log.info("back:{}", path);
  12. httpRequest.getRequestDispatcher(path).forward(httpRequest, servletResponse);
  13. } else {
  14. filterChain.doFilter(servletRequest, servletResponse);
  15. }
  16. }
  17. @Override
  18. public int getOrder() {
  19. return HIGHEST_PRECEDENCE;
  20. }
  21. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题