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

2024-05-24 14:58

本文主要是介绍Spring Cloud Zuul过滤器获取请求参数问题?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

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

qq.png

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

输出日志的Filter

public class DebugRequest extends ZuulFilter {@Overridepublic String filterType() {return "pre";}@Overridepublic int filterOrder() {return 1;}@Overridepublic boolean shouldFilter() {return true;}@Overridepublic 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

 

 

文章推荐

1

大牛坐镇|高端JAVA纯技术群你要加入吗?

2

  赠书|聊聊Akka

3

前后端API交互如何保证数据安全性?

4

知识点-Spring Boot 统一异常处理汇总

5

Spring Boot 1.X和2.X优雅重启实战

 

 

 

这篇关于Spring Cloud Zuul过滤器获取请求参数问题?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/998732

相关文章

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje

C#使用HttpClient进行Post请求出现超时问题的解决及优化

《C#使用HttpClient进行Post请求出现超时问题的解决及优化》最近我的控制台程序发现有时候总是出现请求超时等问题,通常好几分钟最多只有3-4个请求,在使用apipost发现并发10个5分钟也... 目录优化结论单例HttpClient连接池耗尽和并发并发异步最终优化后优化结论我直接上优化结论吧,

SpringBoot使用Apache Tika检测敏感信息

《SpringBoot使用ApacheTika检测敏感信息》ApacheTika是一个功能强大的内容分析工具,它能够从多种文件格式中提取文本、元数据以及其他结构化信息,下面我们来看看如何使用Ap... 目录Tika 主要特性1. 多格式支持2. 自动文件类型检测3. 文本和元数据提取4. 支持 OCR(光学

Java内存泄漏问题的排查、优化与最佳实践

《Java内存泄漏问题的排查、优化与最佳实践》在Java开发中,内存泄漏是一个常见且令人头疼的问题,内存泄漏指的是程序在运行过程中,已经不再使用的对象没有被及时释放,从而导致内存占用不断增加,最终... 目录引言1. 什么是内存泄漏?常见的内存泄漏情况2. 如何排查 Java 中的内存泄漏?2.1 使用 J

JAVA系统中Spring Boot应用程序的配置文件application.yml使用详解

《JAVA系统中SpringBoot应用程序的配置文件application.yml使用详解》:本文主要介绍JAVA系统中SpringBoot应用程序的配置文件application.yml的... 目录文件路径文件内容解释1. Server 配置2. Spring 配置3. Logging 配置4. Ma

MySQL中时区参数time_zone解读

《MySQL中时区参数time_zone解读》MySQL时区参数time_zone用于控制系统函数和字段的DEFAULTCURRENT_TIMESTAMP属性,修改时区可能会影响timestamp类型... 目录前言1.时区参数影响2.如何设置3.字段类型选择总结前言mysql 时区参数 time_zon

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Java 字符数组转字符串的常用方法

《Java字符数组转字符串的常用方法》文章总结了在Java中将字符数组转换为字符串的几种常用方法,包括使用String构造函数、String.valueOf()方法、StringBuilder以及A... 目录1. 使用String构造函数1.1 基本转换方法1.2 注意事项2. 使用String.valu

java脚本使用不同版本jdk的说明介绍

《java脚本使用不同版本jdk的说明介绍》本文介绍了在Java中执行JavaScript脚本的几种方式,包括使用ScriptEngine、Nashorn和GraalVM,ScriptEngine适用... 目录Java脚本使用不同版本jdk的说明1.使用ScriptEngine执行javascript2.

Spring MVC如何设置响应

《SpringMVC如何设置响应》本文介绍了如何在Spring框架中设置响应,并通过不同的注解返回静态页面、HTML片段和JSON数据,此外,还讲解了如何设置响应的状态码和Header... 目录1. 返回静态页面1.1 Spring 默认扫描路径1.2 @RestController2. 返回 html2