Spring Cloud Zuul过滤器获取请求参数问题?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Zuul过滤器获取请求参数问题?相关的知识,希望对你有一定的参考价值。
赠书活动火热进行中,赶快参与吧!详情请查看《聊聊Akka》
今天有个朋友咨询我一个问题,截图如下:
qq.png
qq.png
在忙完了自己的事情后,我也简单的看了下,然后写了一个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
推荐优秀的公众号
「前端桃园」作者是一个喜欢折腾的人,不仅分享前端的知识,也会分享关于认知、学习方法、职场心得以及互联网前沿知识等。不定期分享免费资料,提供头条、小米、饿了么、蘑菇街等各个大厂内推名额,关注到就是赚到。
前端桃园
「程序圈LT」一个半路出家的android 程序猿,维护着一个有态度、有梦想的公众号,工作日每天早上8点准时推送Android、Java技术文章、面试经验、职业道路、资源视频等文章;虽然很多歧视我们半路出家的程序猿,但是我想经过自己的努力得到大家的认可;如果有相同经历的老铁、想一起进步的老铁,欢迎关注我,我们一起学习交流、共同进步;最后再说一遍我们的态度是:每天进步一点点,理想终会被实现!!!
程序圈LT
「JAVA工会」秉承着技术改变世界,程序创造未来的宗旨,专注于java技术分享。为读者提供专业的技术解决方案,架构规划,求职面试等最新讯息。Talk is cheap, show me the code!
JAVA工会
「山子几」AI已来,时不我待,分享Python,分享AI。在这里聚集一群小白互相学习进入,因为分享,所以快乐。
山子几
「JAVA架构师历程」一个专注于为java工程师提供技术干货的文章平台,使其从小牛历练到大拿,在架构师的路上一路前行,共学习,共进步;
JAVA架构师历程
「编程微刊」一个学习编程技术和读者福利共存的公众号,每天推送高质量的优秀博文和原创文章,开源项目,实用工具,面试技巧等 。主编是一位专攻前端的90后程序媛, 我们的目标是做到最好公众号推送平台,服务于所有的程序员开发者 。公众号每月至少准备一次读者送书福利! 欢迎关注!公众号回复“1”,拉你进程序员技术讨论群。
编程微刊
「高永亮的笔记大全」公众号分享一些工作,生活,学习经历,学习心得。一名不羁的砖瓦工,Just coding it,Debug the world!
高永亮的笔记大全
「猿天地」猿天地以程序猿用户为主,提供各种开发相关的技术内容。JAVA方向为主(Spring Boot, Spring Cloud,Sharding JDBC,Elastic Job,Mongodb等)。
互联网时代,技术从未如此简单,我们一起前行。
猿天地
文章推荐
1大牛坐镇|高端JAVA纯技术群你要加入吗?
2 赠书|聊聊Akka
3前后端API交互如何保证数据安全性?
4知识点-Spring Boot 统一异常处理汇总
5Spring Boot 1.X和2.X优雅重启实战
更多技术分享尽在微信群,加群请关注公众号,点击加群按钮。
以上是关于Spring Cloud Zuul过滤器获取请求参数问题?的主要内容,如果未能解决你的问题,请参考以下文章
spring cloud zuul filter 设置过滤请求
7Spring -Cloud-路由网管Spring Cloud Zuul