在Spring Boot 2迁移后使用Filter进行DispatcherServlet转发

时间:2018-09-17 15:00:03

标签: java spring spring-mvc spring-boot tomcat

我最近将应用程序从Spring Boot 1.5.14更新到了Spring Boot2。在使用自定义过滤器拦截请求后转发请求时遇到问题。

CustomFilter.java

@Override
protected void doFilterInternal(HttpServletRequest req, HttpServletResponse res, FilterChain filterChain) throws ServletException, IOException {
    HeaderMapRequestWrapper requestWrapper = new HeaderMapRequestWrapper(req);
    requestWrapper.addHeader(SecurityConstants.X_ADAP_HEADER,"true");
    requestWrapper.getRequestDispatcher(RestApi.NEW_LOGIN_TOKEN).forward(requestWrapper, res);
}

转发更新后的请求后,出现以下错误:

2018-09-17 17:40:34.448  WARN 28704 --- [-nio-443-exec-5] o.s.web.servlet.PageNotFound             : Request method 'POST' not supported
2018-09-17 17:40:34.449  WARN 28704 --- [-nio-443-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

在迁移过程中,我没有对这部分代码进行任何更改,但是在使用Spring Boot 1.5.14时,此方法工作正常。

进一步调试后,我发现在堆栈跟踪的更深处,我到达了checkRequest(HttpServletRequest request)类中的WebContentGenerator方法。

WebContentGenerator.java

protected final void checkRequest(HttpServletRequest request) throws ServletException {
    // Check whether we should support the request method.
    String method = request.getMethod();
    if (this.supportedMethods != null && !this.supportedMethods.contains(method)) {
        throw new HttpRequestMethodNotSupportedException(method, this.supportedMethods);
    }

    // Check whether a session is required.
    if (this.requireSession && request.getSession(false) == null) {
        throw new HttpSessionRequiredException("Pre-existing session required but none found");
    }
}

在此方法中,supportedMethods等于{GET, HEAD},因此我收到此错误。

在转发请求后检查我的Spring Boot 1.5.14项目时,我什至没有到达WebContentGenerator类,因此这使我认为这与Spring 2迁移期间的更新类有关。 Spring Boot 2中发生了什么变化,使我得到此错误,以及如何解决此问题?

感谢并感谢您的帮助!

0 个答案:

没有答案