gateway的学习

1.网关的作用

1.负载均衡

2.过滤器的使用

1.通过配置文件实现的过滤器

2.代码逻辑层面实现全局过滤器

java 复制代码
//全局过滤器代码逻辑实现
@Component
//@Order(1):注解配置过滤器的执行顺序
public class GlobalFilter implements GatewayFilter, Ordered {
    /**
     * 处理当前请求,有必要的话通过 {@link GatewayFilterChain} 将请求交给下一个过滤器处理
     *
     * @param exchange 请求上下文,里面可以获取 Request、Response 等信息
     * @param chain 用来把请求委托给下一个过滤器
     * @return {@code Mono<Void>} 返回值标示当前过滤器业务结束
     */

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        MultiValueMap<String, String> params = request.getQueryParams();
        String token = params.getFirst("token");
        if("token".equals(token)){
            //放行到下一个过滤器
            return chain.filter(exchange);
        }
        //401:登陆失败
        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
        return exchange.getResponse().setComplete();
    }

    //代码配置过滤器顺序
    @Override
    public int getOrder() {
        return 1;
    }

}

3.过滤器的执行顺序

4.网关跨域处理

相关推荐
你好潘先生8 小时前
别再记命令了,用 yeero do 说句人话就能跑脚本,而且不烧 token
服务器·python·命令行
orion5719 小时前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221611 天前
Linux驱动编译与加载
linux·嵌入式
程序员老赵1 天前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
vivo互联网技术1 天前
从 10 分钟到 1 秒:ES 深度分页任意跳页的三轮优化实战
服务器·数据库·redis·elasticsearch·深度分页
用户805533698031 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698031 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux