近几天打算做一个网关校验的东西,但是发现在使用Gateway的过滤器的时候,没找到设置响应的状态码,网上找了好多也没找到emmmm,不过还好解决了。

直接贴上代码,下面有给出:

package com.llq.filter;import com.alibaba.fastjson.JSON;
import com.llq.pojo.ResponseData;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;@Component
public class TokenFilter implements GlobalFilter, Ordered {private static final Logger LOGGER = LoggerFactory.getLogger(TokenFilter.class);@Beanpublic GlobalFilter registryTokenFilter() {return new TokenFilter();}@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpRequest serverHttpRequest = exchange.getRequest();ServerHttpResponse serverHttpResponse = exchange.getResponse();String token = exchange.getRequest().getHeaders().getFirst("token");LOGGER.info("current request URI : {}", serverHttpRequest.getURI());if (StringUtils.isBlank(token)) {serverHttpResponse.getHeaders().add("Content-Type", "application/json;charset=UTF-8");ResponseData responseResult = ResponseData.builder().code(401).msg("未授权").build();DataBuffer dataBuffer = serverHttpResponse.bufferFactory().wrap(JSON.toJSONString(responseResult).getBytes());serverHttpResponse.setStatusCode(HttpStatus.UNAUTHORIZED); // 设置响应状态码 401, 200, 503等等return serverHttpResponse.writeWith(Flux.just(dataBuffer));}return chain.filter(exchange);}@Overridepublic int getOrder() {return -1;}
}

Spring Cloud Gateway 自定义网络响应状态码(401,500,503等等)相关推荐

  1. Spring cloud gateway的自定义异常响应

    由于gateway默认的返回码和返回信息不能满足下游微服务的自定义的异常响应码和描述,则需要通过网关透传下游微服务的异常的码值或者至少异常描述信息. spring cloud gateway给我们留了 ...

  2. 4. Spring Cloud Gateway自定义Filter

    1. Filter介绍 Filter是实现在网关路由之前或之后加入一些逻辑,或者拦截过滤的工具, 称为过滤器. 在Spring Cloud Gateway中除了内置的Filter之外,也可以自定义Fi ...

  3. 关于使用Spring Cloud Gateway网关网络连接的4个重要的参数配置

    整理出这四个重要的参数,说起来很不易,来源于一次网络故障事故后的调查,对于平时使用Spring Cloud Gateway(简称scg)来说这些参数几乎很少会关注到,从网上也很少能看到讲解的文章,表面 ...

  4. Spring Cloud Gateway自定义Filter

    今天我们来研究自定义Filter,自定义的Filter都是要实现GatewayFilter接口,如果需要指定排序则同时实现Order接口.filter有前置过滤器和后置过滤器,一般情况下,后置过滤器在 ...

  5. 【云原生微服务>SCG网关篇十二】Spring Cloud Gateway集成Sentinel API实现多种限流方式

    文章目录 一.前言 二.Gateway集成Sentinel API 0.集成Sentinel的核心概念 1)GatewayFlowRule 和 ApiDefinition 2)GatewayFlowR ...

  6. 微服务 API 网关架构演进 Spring Cloud Gateway ShenYu APISIX

    目录 后台服务网关 Spring Cloud Gateway 遇到问题 Apache ShenYu Higress fizz-gateway-community 企业案例 前台流量网关 APISIX ...

  7. 微服务网关Zuul迁移到Spring Cloud Gateway

    https://juejin.im/post/5ba8daa56fb9a05cfe486ebf 背景 在之前的文章中,我们介绍过微服务网关Spring Cloud Netflix Zuul,前段时间有 ...

  8. Spring Cloud Gateway 动态路由管理,一点都不吹,应该没有比这更好的管理系统了吧

      本文介绍的 Spring Cloud Gateway 动态路由.不比其他博客通篇 copy 的 Gateway 动态路由,直接上干货!!!为你们提供了一套完整的动态路由管理系统.文末附本文全套代码 ...

  9. Spring Cloud Gateway一次请求调用源码解析

    简介: 最近通过深入学习Spring Cloud Gateway发现这个框架的架构设计非常简单.有效,很多组件的设计都非常值得学习,本文就Spring Cloud Gateway做一个简单的介绍,以及 ...

最新文章

  1. 简单配置Nginx的脚本启动
  2. 卓语言对泛型类的使用
  3. python人工智能面试题_人工智能面试题分享(含答案)
  4. php拼接多个insert,php – 将多个INSERTS分成一个表和多个表
  5. Android应用程序消息处理机制(Looper、Handler)分析
  6. AngularJS 国际化 支持记录语言刷新后语言不变
  7. 最大子序列求和_最大连续子序列和
  8. 看我如何发现开源 WAF引擎ModSecurity 中的DoS 漏洞
  9. Acwing第 38 场周赛
  10. [Leetcode]设计链表
  11. 迁安职中计算机专业,迁安职业技术教育中心2021年招生简章
  12. DICOM获取worklist患者列表抓包分析
  13. 袋鼠云产品功能更新报告01期丨用诚心倾听您的需求
  14. C语言中用零作为终止标记,求出一批非零整数中的偶数.奇数的平均值,用零作为终止标记的程序...
  15. 球球大佬们帮忙分析win10蓝屏dmp!!
  16. 王小川谈创业必须突破的六大槛:人、产品、渠道、体制、资本、移动
  17. 读书笔记_小米创业思考
  18. FXLMS主动降噪的simulink建模与仿真
  19. PIE SDK水体指数法
  20. UG NX 12 基准平面

热门文章

  1. js 导出excel,设置下载的标题
  2. 如何把catia完全卸载干净_catia卸载不了怎么解决-解决catia无法卸载的方法 - 河东软件园...
  3. RHCE7-NOTE(红帽工程师--题库详细笔记)
  4. HCNP——LSR报文、LSU报文及LSAck报文
  5. 201912月灵感记录
  6. Python 把两张图片拼起来
  7. Arduino UNO驱动ADS1115模数转换模块
  8. 夏令时和时区最佳实践
  9. golang牛牛算法规则与聊天功能
  10. JAVA继承面积计算体积_java初学基础 使用继承 计算圆形周长、面积和圆柱体积...