说说背景:假如有一个用户服在用户登录后,生成一个token给到客户端,用户每次请求时都需要这个token,于是每次都会在网关 gateway 校验,校验通过后网关从token中解析出userId,然后将userId送到各个服务。

比如现在有一个 java 服务 和 一个 php 服务,从网关访问的URL 分别是 http://127.0.0.1:8201/java/ 和 http://127.0.0.1:8201/php/,现在暂时只需对 php 这个服务验证,先看效果图

spring cloud gateway 的官网文档地址:http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html#_addrequestheader_gatewayfilter_factory

一、需要自定义 GatewayFilterFactory 继承  AbstractGatewayFilterFactory 抽象类,代码如下:

package cn.taxiong.tx_api_gateway_server.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono; /**
* JWT验证的过滤器
*
* @author szliugx@gmail.com
* @create 2018-09-09 下午10:05
**/
public class JwtCheckGatewayFilterFactory extends AbstractGatewayFilterFactory<JwtCheckGatewayFilterFactory.Config> { public JwtCheckGatewayFilterFactory() {
super(Config.class);
} @Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String jwtToken = exchange.getRequest().getHeaders().getFirst("Authorization");
//校验jwtToken的合法性
if (jwtToken != null) {
// 合法
// 将用户id作为参数传递下去
return chain.filter(exchange);
} //不合法(响应未登录的异常)
ServerHttpResponse response = exchange.getResponse();
//设置headers
HttpHeaders httpHeaders = response.getHeaders();
httpHeaders.add("Content-Type", "application/json; charset=UTF-8");
httpHeaders.add("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
//设置body
String warningStr = "未登录或登录超时";
DataBuffer bodyDataBuffer = response.bufferFactory().wrap(warningStr.getBytes()); return response.writeWith(Mono.just(bodyDataBuffer));
};
} public static class Config {
//Put the configuration properties for your filter here
}
}

二、需要将自定义的 GatewayFilterFactory 注入到Spring 中

package cn.taxiong.tx_api_gateway_server.config;

import cn.taxiong.tx_api_gateway_server.filter.JwtCheckGatewayFilterFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* 应用配置
*
* @author szliugx@gmail.com
* @create 2018-09-09 下午10:57
**/
@Configuration
public class AppConfig {
@Bean
public JwtCheckGatewayFilterFactory jwtCheckGatewayFilterFactory(){
return new JwtCheckGatewayFilterFactory();
}
}

三、网关服务的配置文件中配置 自定义过滤器 生效的服务

这里只配置了 php 这个服务,java 这个服务不使用这个过滤器规则

SpringCloud初体验:七、gateway 网关服务如何做token验证的更多相关文章

  1. SpringCloud初体验:五、Sidecar 将 PHP 这类非 Java 生态语言的服务接入 Spring Cloud

    先起一个 Sidecar 服务,一个PHP服务一个应用,和PHP服务部署在同一台机子,通过 localhost 访问,这样就解决了网络开销,相当于本地进程间调用 Sidecar 服务比较简单, 1.这 ...

  2. Spring Cloud gateway 网关服务二 断言、过滤器

    微服务当前这么火爆的程度,如果不能学会一种微服务框架技术.怎么能升职加薪,增加简历的筹码?spring cloud 和 Dubbo 需要单独学习.说没有时间?没有精力?要学俩个框架?而Spring C ...

  3. SpringCloud初体验:四、API GateWay 服务网关

    网关服务很多,比如:Zuul.Kong.spring cloud gateway ……, 这里不纠结哪种性能好,本次体验是用的 spring cloud gateway 更多网关比较可以了解这篇文章: ...

  4. SpringCloud初体验:前言

    体验了一天 SpringCloud 后发现,人们所讲的微服务架构不是一门技术,而是一种风格. 感觉确实可以这么认同,因为一套 SpringCloud 玩下来(未深入.未完整「链路追踪.动态刷新配置…… ...

  5. SpringCloud(四)GateWay网关

    GateWay网关 概述简介 Gateway是在 Spring生态系统之上构建的AP网关服务,基于 Spring5, Spring Boot2和 Project Reactor等技术. Gateway ...

  6. 关于Ocelot和Consul 实现GateWay(网关) 服务注册 负载均衡等方面

    Ocelot   路由  请求聚合  服务发现 认证  鉴权 限流熔断 内置负载均衡器 Consul   自动服务发现    健康检查 通过Ocelot搭建API网关   服务注册   负载均衡 1. ...

  7. Spring Cloud gateway 网关服务 一

    之前我们介绍了 zuul网关服务,今天聊聊spring cloud gateway 作为spring cloud的亲儿子网关服务.很多的想法都是参照zuul,为了考虑zuul 迁移到gateway 提 ...

  8. springCloud学习05之api网关服务zuul过滤器filter

    前面学习了zuul的反向代理.负载均衡.fallback回退.这张学习写过滤器filter,做java web开发的对filter都不陌生,那就是客户端(如浏览器)发起请求的时候,都先经过过滤器fil ...

  9. SpringCloud初体验:六、利用 Sleuth 和 Zipkin 给微服务加上链路监控追踪查看功能

    首先:装上 Zipkin 服务,收集调用链跟踪数据,体验时装在了本机docker上, 方便快捷 docker run -d -p : openzipkin/zipkin 安装后访问地址也是 9411端 ...

随机推荐

  1. js中JSON.stringify用于自定义的类

    参考:http://stackoverflow.com/questions/7356694/how-to-json-stringify-a-user-defined-class-in-javascri ...

  2. 简话Angular 05 Angular表单验证

    一句话: 可以使用所有html5表单验证功能,同时Angular还增强了部分验证,支持动态验证 1. 上源码 <div ng-controller="ExampleController ...

  3. CAS 服务端数据库认证

    CAS-服务端数据库认证 数据认证需要相关的jar包: cas-server-support-jdbc-x.x.x.jar MySQL-connector-Java-x.x.x-bin.jar 修改C ...

  4. java 继承 初始化顺序

    面向对象三大特性: 封装,继承,多态 java 继承初始化顺序 先初始化父类对象, 在父类对象中先初始化父类属性,再初始化父类的构造方法,然后初始化子类对象,初始化子类对象的属性,初始化子类的构造方法 ...

  5. bacula配置

    Bacula Bacula是一款开源的跨平台网络备份工具,提供基于企业级的CS的备份解决方案.可以对数据进行备份.恢复.以及完整性校验.  功能特点: 支持完全备份,增量备份,差异备份. 支持多种恢复 ...

  6. iOS UIlabel怎么加载html字符串 富文本的用法

    要加载html字符串,用人说,直接用webView啊!但是,有时候我们只需要显示2行文字,如此少的内容却要在复杂的UI排版中加入一个占用资源较多的webview,得不偿失.这里要说的是,我们其实可以用 ...

  7. DevExpress v18.1新版亮点——ASP.NET Bootstrap篇(一)

    用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress ASP.NET Bootstrap v18.1 的新功能,快 ...

  8. Vue 相关难点汇总

    1. 父子组件的双向数据绑定,所以在子组件是不允许修改父组件的属性的. // 解决办法 // 在子组件data中定义一个父组件传递过来的副本,再把该副本利用this.$emit("" ...

  9. Alpha阶段敏捷冲刺---Day6

    一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 今天的任务标志着我们项目进入收尾阶段,今天将完成大部分程序的功能,例如主界面设计,彻底完成计算模块,服务器随机生成题目等等,这些任 ...

  10. parser_url

    $url="http://127.0.0.1/test2.php?sitename=mysite.cn&a=1&b=2";$a=parse_url($url);p( ...