简体   繁体   English

在过滤器的dofilter方法中获取httpservletrequest

[英]get httpservletrequest inside dofilter method of filter

I am using Filter to intercept each request, where in filter I can get only ServletRequest inputstream, which is returning empty input stream, to get the input stream.It seems that I need to get the HttpServletRequest. 我正在使用Filter拦截每个请求,其中在filter中我只能获取ServletRequest输入流,该输入流返回空输入流,以获取输入流。似乎我需要获取HttpServletRequest。 How to achieve this? 如何实现呢? I tried casting as well as converting to httpservletrequest. 我尝试了转换以及转换为httpservletrequest。 none of the ways, i was able to get value using 没有办法,我能够使用获得价值

request.getParameter("aaa") //when request content type is multipart/file.

getInputStream() is a part of ServletRequest interface, so even if you cast ServletRequest to HttpServletRequest ( which extends from ServletRequest) the result will be the same. getInputStream()ServletRequest接口的一部分,因此,即使将ServletRequestHttpServletRequest (从ServletRequest扩展而来),结果也将相同。

Perhaps this answer will help you to find out why inputstream is empty. 也许这个答案将帮助您找出为什么inputstream为空。

If the input stream is empty there was no request body. 如果输入流为空,则没有请求正文。 Getting the HttpServletRequest won't change that: in fact, you already have it. 获取HttpServletRequest不会改变这一点:实际上,您已经拥有了它。

I was successfully able to type cast the "servletRequest" into "HttpServletRequest" inside my custom filter class. 我成功地将自定义过滤器类中的“ servletRequest”类型转换为“ HttpServletRequest”。 Below is the snippet, 以下是代码段,

public class SAMLFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

        if(servletRequest instanceof HttpServletRequest) {
            httpServletRequest = (HttpServletRequest)servletRequest;
            System.out.println("getInputStream = " + httpServletRequest.getInputStream());

        } else {
            System.out.println("NOT CAST");
        }


    @Override
    public void destroy() {

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM