我在应用程序中引入了过滤器以进行验证。当我调用 doFilter 方法时,
public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse,FilterChain filterChain)
我无法使用getParts()
在doFilter()
。 getParts()
可用于HttpServletRequest
.
即使我输入来自 ServletRequest
的cast至HttpServletRequest
,我得到空值。
问题是,有什么办法可以访问 HttpServletRequest
里面doFilter()
没有来自 ServletRequest
的类型转换?或者如何直接访问HttpServletRequest
里面doFilter()
而不将其传递到方法中?
下面的代码片段我收到错误
for(final Part part : ((HttpServletRequest) servletRequest).getParts())
最佳答案
在 Tomcat 中,这是一个已知但已解决的问题。请参阅此错误报告:https://bz.apache.org/bugzilla/show_bug.cgi?id=49711
现在有一个选项 (allowCasualMultipartParsing
) 可以将 Tomcat 配置为允许 getParts()
调用 servlet 过滤器(默认为关闭)。请参阅documentation :
allowCasualMultipartParsing
: Set to true if Tomcat should automatically parse multipart/form-data request bodies when HttpServletRequest.getPart* or HttpServletRequest.getParameter* is called, even when the target servlet isn't marked with the @MultipartConfig annotation (See Servlet Specification 3.0, Section 3.2 for details). Note that any setting other than false causes Tomcat to behave in a way that is not technically spec-compliant. The default is false
关于java - 在 doFilter 中获取 httpservletrequest,无需从 ServletRequest 进行类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503961/