Spring Cloud Zuul过滤器获取请求参数问题?

今天有个朋友咨询我一个问题,截图如下:

Spring Cloud Zuul过滤器获取请求参数问题?

在忙完了自己的事情后,我也简单的看了下,然后写了一个Filter来实现这位朋友说的功能,其实就是想记录请求的参数信息,下面我把涉及到的请求都打印了一遍,当然日志格式你可以自定义。

输出日志的Filter

public class DebugRequest extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        HttpServletRequest req = (HttpServletRequest)RequestContext.getCurrentContext().getRequest();
        System.err.println("REQUEST:: " + req.getScheme() + " " + req.getRemoteAddr() + ":" + req.getRemotePort());
        System.err.println("REQUEST:: " + req.getScheme() + " " + req.getRemoteAddr() + ":" + req.getRemotePort());
        StringBuilder params = new StringBuilder("?");
        Enumeration<String> names = req.getParameterNames();
        if( req.getMethod().equals("GET") ) {
    	   while (names.hasMoreElements()) {
	   		  String name = (String) names.nextElement();
	   		  params.append(name);
	   		  params.append("=");
	   		  params.append(req.getParameter(name));
	   		  params.append("&");
   		   }
        }
       
        if (params.length() > 0) {
        	params.delete(params.length()-1, params.length());
        }
        System.err.println("REQUEST:: > " + req.getMethod() + " " + req.getRequestURI() + params + " " + req.getProtocol());

        Enumeration<String> headers = req.getHeaderNames();

        while (headers.hasMoreElements()) {
            String name = (String) headers.nextElement();
            String value = req.getHeader(name);
            System.err.println("REQUEST:: > " + name + ":" + value);
        }

        final RequestContext ctx = RequestContext.getCurrentContext();
        if (!ctx.isChunkedRequestBody()) {
            ServletInputStream inp = null;
            try {
                inp = ctx.getRequest().getInputStream();
                String body = null;
                if (inp != null) {
                	body = IOUtils.toString(inp);
                	System.err.println("REQUEST:: > " + body);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

}

GET 有参数

REQUEST:: http 0:0:0:0:0:0:0:1:61689
REQUEST:: http 0:0:0:0:0:0:0:1:61689
REQUEST:: > GET /fsh-house/house/1?name=玉田新村 HTTP/1.1
REQUEST:: > host:localhost:2103
REQUEST:: > connection:keep-alive
REQUEST:: > cache-control:max-age=0
REQUEST:: > upgrade-insecure-requests:1
REQUEST:: > user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
REQUEST:: > accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
REQUEST:: > accept-encoding:gzip, deflate, br
REQUEST:: > accept-language:zh-CN,zh;q=0.9
REQUEST:: > cookie:fsh_split=_; fsh_login_mobile=18812341234; UM_distinctid=16367d1273c13e-0438eaf56e7d35-2d604637-3d10d-16367d1273d40e; CNZZDATA1269429164=99959605-1526454826-http%253A%252F%252Flocalhost%253A8084%252F%7C1526454826; fsh_token=6EA5BBAD0DA0F14091A661CA11DE46862DF155C38FDF69F41971F953ADAB4AD9D57BF39545A13FC873480EEA52032C46DF43FCD125F5DD6BAC73137BECF7A1290763389FD37651457E3C78530D2FF70F40AAE1580AD9A4EC828CD9B10F2A0B31E9329BDB523627A6461265B775EA9A67995A7F9531AEFB7455A785F0B04F8158; fsh_modul=1; cn_1269429164_dplus=%7B%22distinct_id%22%3A%20%2216367d1273c13e-0438eaf56e7d35-2d604637-3d10d-16367d1273d40e%22%2C%22sp%22%3A%20%7B%22%24_sessionid%22%3A%200%2C%22%24_sessionTime%22%3A%201526457128%2C%22%24dp%22%3A%200%2C%22%24_sessionPVTime%22%3A%201526457128%7D%7D; _ga=GA1.1.194908057.1528255075; _gid=GA1.1.981282073.1528255075
REQUEST:: >

GET 无参数

REQUEST:: http 0:0:0:0:0:0:0:1:56671
REQUEST:: http 0:0:0:0:0:0:0:1:56671
REQUEST:: > GET /fsh-house/house/1 HTTP/1.1
REQUEST:: > host:localhost:2103
REQUEST:: > connection:keep-alive
REQUEST:: > cache-control:max-age=0
REQUEST:: > upgrade-insecure-requests:1
REQUEST:: > user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
REQUEST:: > accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
REQUEST:: > accept-encoding:gzip, deflate, br
REQUEST:: > accept-language:zh-CN,zh;q=0.9
REQUEST:: > cookie:fsh_split=_; fsh_login_mobile=18812341234; UM_distinctid=16367d1273c13e-0438eaf56e7d35-2d604637-3d10d-16367d1273d40e; CNZZDATA1269429164=99959605-1526454826-http%253A%252F%252Flocalhost%253A8084%252F%7C1526454826; fsh_token=6EA5BBAD0DA0F14091A661CA11DE46862DF155C38FDF69F41971F953ADAB4AD9D57BF39545A13FC873480EEA52032C46DF43FCD125F5DD6BAC73137BECF7A1290763389FD37651457E3C78530D2FF70F40AAE1580AD9A4EC828CD9B10F2A0B31E9329BDB523627A6461265B775EA9A67995A7F9531AEFB7455A785F0B04F8158; fsh_modul=1; cn_1269429164_dplus=%7B%22distinct_id%22%3A%20%2216367d1273c13e-0438eaf56e7d35-2d604637-3d10d-16367d1273d40e%22%2C%22sp%22%3A%20%7B%22%24_sessionid%22%3A%200%2C%22%24_sessionTime%22%3A%201526457128%2C%22%24dp%22%3A%200%2C%22%24_sessionPVTime%22%3A%201526457128%7D%7D; _ga=GA1.1.194908057.1528255075; _gid=GA1.1.981282073.1528255075
REQUEST:: >

POST JSON

REQUEST:: http 0:0:0:0:0:0:0:1:54327
REQUEST:: http 0:0:0:0:0:0:0:1:54327
REQUEST:: > POST /fsh-house/house/save HTTP/1.1
REQUEST:: > host:localhost:2103
REQUEST:: > connection:keep-alive
REQUEST:: > content-length:74
REQUEST:: > user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
REQUEST:: > cache-control:no-cache
REQUEST:: > origin:chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
REQUEST:: > postman-token:dfe06ff3-5449-599f-6960-8d1851f7defd
REQUEST:: > content-type:application/json
REQUEST:: > accept:*/*
REQUEST:: > accept-encoding:gzip, deflate, br
REQUEST:: > accept-language:zh-CN,zh;q=0.9
REQUEST:: > {
	"id": 1,
"city": "上海",
"region": "虹口",
"name": "玉田新村"
}

POST form-data

REQUEST:: http 0:0:0:0:0:0:0:1:64201
REQUEST:: http 0:0:0:0:0:0:0:1:64201
REQUEST:: > POST /fsh-house/house/save HTTP/1.1
REQUEST:: > host:localhost:2103
REQUEST:: > connection:keep-alive
REQUEST:: > content-length:244
REQUEST:: > user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
REQUEST:: > cache-control:no-cache
REQUEST:: > origin:chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
REQUEST:: > postman-token:2fd5f87a-f160-b42b-ea3a-261173cae9d3
REQUEST:: > content-type:application/x-www-form-urlencoded
REQUEST:: > accept:*/*
REQUEST:: > accept-encoding:gzip, deflate, br
REQUEST:: > accept-language:zh-CN,zh;q=0.9
REQUEST:: > ------WebKitFormBoundary0494suClBOKyocis%0D%0AContent-Disposition%3A+form-data%3B+name=%22name%22%0D%0A%0D%0A%E7%8E%89%E7%94%B0%E6%96%B0%E6%9D%91%0D%0A------WebKitFormBoundary0494suClBOKyocis%0D%0AContent-Disposition%3A+form-data%3B+name%3D%22city%22%0D%0A%0D%0A%E4%B8%8A%E6%B5%B7%0D%0A------WebKitFormBoundary0494suClBOKyocis--%0D%0A

POST x-www-form-urlencoded

REQUEST:: http 0:0:0:0:0:0:0:1:64379
REQUEST:: http 0:0:0:0:0:0:0:1:64379
REQUEST:: > POST /fsh-house/house/save HTTP/1.1
REQUEST:: > host:localhost:2103
REQUEST:: > connection:keep-alive
REQUEST:: > content-length:65
REQUEST:: > user-agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
REQUEST:: > cache-control:no-cache
REQUEST:: > origin:chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
REQUEST:: > postman-token:17176086-08dd-900b-6df0-79de98273a95
REQUEST:: > content-type:application/x-www-form-urlencoded
REQUEST:: > accept:*/*
REQUEST:: > accept-encoding:gzip, deflate, br
REQUEST:: > accept-language:zh-CN,zh;q=0.9
REQUEST:: > city=%E4%B8%8A%E6%B5%B7&name=%E7%8E%89%E7%94%B0%E6%96%B0%E6%9D%91

更多技术分享请关注微信公众号:猿天地

Spring Cloud Zuul过滤器获取请求参数问题?

原文 

https://juejin.im/post/5b1f369f5188257d3c037ddf

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Spring Cloud Zuul过滤器获取请求参数问题?

分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址