专栏首页猿天地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

本文分享自微信公众号 - 猿天地(cxytiandi)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2018-06-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 我在外包公司做增删改查有前途么?

    这是我无意中在筛选简历时,看到一位朋友发布的求职说明中,明确指出,外包勿扰,并给出了他做出这个决定的理由:

    猿天地
  • spring boot中删除Data MongoDB的_class

    在使用data mongodb插入数据的时候,插入后文档中会多出一个_class. 定义实体类 public class User { String ...

    猿天地
  • 一次大量删除导致 MySQL 慢查的分析

    当慢查在执行的时候,大部分的都是表现在 Sending data 的状态,我们通过 profiling 去确认下慢查的时间分布:

    猿天地
  • 剑指Offer面试题:28.连续子数组的最大和

      这个题目在我去年参加校园招聘时,某公司的二面采用了机试,而题目刚好就是这道题。一般看到这道题目就会想到枚举出数组的所有子数组并求出它们的和。一个长度为n的数...

    Edison Zhou
  • 一小时掌握R语言数据可视化

    ggplot2是R语言最为强大的作图软件包,有着自成一派的可视化理念,数据可视化是数据分析的重要一步,让我们通过由浅入深的掌握数据可视化的精髓。 请尊重原创,转...

    小莹莹
  • bigpipe性能优化

    本文作者:IMWeb moonye 原文出处:IMWeb社区 未经同意,禁止转载 背景 当前网速越来越快,但是随着网页内容越来越丰富,其实我们打开网页...

    IMWeb前端团队
  • Vue学习(二)数据事件和方法

    例如:v-text(会进行一次转义,相比与v-html), v-html(不会转义)

    一觉睡到小时候
  • bigpipe性能优化

    当前网速越来越快,但是随着网页内容越来越丰富,其实我们打开网页的速度并未得到什么提升,相反,过多的内容会导致网页打开速度变慢。于是,出现了一些性能优化的方法。 ...

    IMWeb前端团队
  • LDAP用户组信息异常导致Sentry授权失效问题分析

    问题起因:“80040151”用户反映在Hue上通过Hive查询表数据出现异常,异常信息为权限拒绝,需要通过Sentry授权。然而通过beeline直连Hive...

    Fayson
  • r语言ggplot2包学习笔记(上代码+注释)

    #ggplot2学习笔记##第一节:尝试ggplot library(ggplot2) #使用的是R内置数据(mpg) qplot(displ,hwy,data...

    学到老

扫码关注云+社区

领取腾讯云代金券