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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu