当前位置: 首页 > news >正文

Gateway网关:路由和鉴权

网关的核心功能是:路由和过滤(鉴权)

  •  搭建网关
<dependencies><!--引入gateway 网关--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency>
</dependencies>
  • 编写启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ApiGatewayApplication {public static void main(String[] args) {SpringApplication.run(ApiGatewayApplication.class,args);}}
  • 编写配置
server:port: 10010
spring:application:name: api-gatewaycloud:# 网关配置gateway:# 路由配置:转发规则routes: #集合。# id: 唯一标识。默认是一个UUID# uri: 转发路径# predicates: 条件,用于请求网关路径的匹配规则- id: userserviceuri: http://localhost:8081/predicates:- Path=/user/**

application.yml 中的uri是写死的,就是Gateway-静态路由

Gateway-动态路由

<dependency><--添加注册中心Eureka--> <groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 修改配置文件

server:port: 10010
spring:application:name: api-gatewaycloud:# 网关配置gateway:# 路由配置:转发规则routes: #集合。# id: 唯一标识。默认是一个UUID# uri: 转发路径# predicates: 条件,用于请求网关路径的匹配规则- id: user-serviceuri: lb://userservice # lb负载均衡predicates:- Path=/user/**eureka:client:service-url:defaultZone: http://127.0.0.1:10086/eureka

Gateway-过滤器

GatewayFilter:局部过滤器,针对单个路由 GlobalFilter :全局过滤器,针对所有路由

全局过滤器

  • GlobalFilter 全局过滤器,不需要在配置文件中配置,系统初始化时加载,并作用在每个路由上。

  • Spring Cloud Gateway 核心的功能也是通过内置的全局过滤器来完成。

  • 自定义全局过滤器步骤:

    1. 定义类实现 GlobalFilter 和 Ordered接口

    2. 复写方法

    3. 完成逻辑处理

/*** 自定义全局过滤器*/
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {//需求 :请求参数中带有token 内容,就放行,否则提示未授权@Overridepublic Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        获取请求对象ServerHttpRequest request = exchange.getRequest();
//        获取参数MultiValueMap<String, String> valueMap = request.getQueryParams();List<String> valList = valueMap.get("token");if(CollectionUtils.isEmpty(valList)){//异常情况,没有token参数// 获取响应对象ServerHttpResponse response = exchange.getResponse();// 返回响应码response.setStatusCode(HttpStatus.FORBIDDEN);//401// 完成响应return response.setComplete();}return chain.filter(exchange);//放行}@Overridepublic int getOrder() {return 0;}
}

http://www.dtcms.com/a/172445.html

相关文章:

  • Android控件View、ImageView、WebView用法
  • QT 在圆的边界画出圆
  • Python打造智能化多目标车辆跟踪系统:从理论到实践
  • LeetCode 热题 100 70. 爬楼梯
  • python读取图片自动旋转的问题解决
  • 深入解析:删除有序数组中的重复项 II——巧用双指针实现条件筛选
  • 【Leetcode 每日一题 - 补卡】838. 推多米诺
  • 掌握流量管理:利用 EKS Ingress 和 AWS 负载均衡器控制器
  • 用户模块 - IP归属地技术方案
  • TCP/IP协议深度解析:从分层架构到TCP核心机制
  • MySQL 复合查询
  • Spring AMQP源码解析
  • 英伟达语音识别模型论文速读:Fast Conformer
  • MongoDB入门详解
  • \documentclass[lettersize,journal]{IEEEtran}什么意思
  • 【计算机视觉】三维重建:tiny-cuda-nn:高性能神经网络推理与训练的CUDA加速库
  • n8n中Wait节点的使用详解:流程暂停与恢复的实战指南
  • K8S node ARP 表爆满 如何优化
  • DeepSeek-Prover-V2:数学定理证明领域的新突破
  • 20、数据可视化:魔镜报表——React 19 图表集成
  • 解决因字段过长使MYSQL数据解析超时导致线上CPU告警问题
  • 浅拷贝和深拷贝的区别
  • css使用aspect-ratio制作4:3和9:16和1:1等等比例布局
  • 华为云Astro大屏中桥接器、连接器、转化器以及数据源、数据集、资产管理等概念梳理
  • Leetcode刷题记录30——螺旋矩阵
  • linux-文件操作
  • FreeRTOS菜鸟入门(十一)·信号量·二值、计数、递归以及互斥信号量的区别·优先级翻转以及继承机制详解
  • 基于MATLAB图像中的圆形目标识别和标记
  • MCUboot 中的 BOOT_SWAP_TYPE_PERM 功能介绍
  • 2048游戏(含Python源码)