> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/ji-chu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/ji-chu/spring/group-1/spring-cloud/wang-guan-spring-cloud-gateway/spring-cloud-gateway/wei-ci-he-guolqi-gong-chang/zi-ding-yi-guolqi.md).

# 自定义过滤器

Spring Cloud Gateway允许我们通过在网关中使用过滤器来构建自定义逻辑。

Spring Cloud Gateway支持以下两种类型的过滤器：

* **预过滤器：**&#x5728;实际请求发送到目标目的地之前调用预过滤器。预过滤器通常执行预处理任务，例如确保服务具有一致的消息格式（例如，关键的HTTP头已设置）或充当门卫以确保调用服务的用户已经进行了身份验证（他们确实是他们所声称的用户）。
* **后过滤器：**&#x5728;目标服务处理请求并将响应发送回客户端后调用后过滤器。通常，我们会实现后过滤器以记录来自目标服务的响应，处理错误或审计响应以获取敏感信息。

> ## <mark style="color:orange;">需要注意的是，预过滤器无法将用户重定向到不同的端点或服务。</mark>
