怎么为春云网关加上全局过滤器

  介绍

本篇内容介绍了“怎么为春云网关加上全局过滤器”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

既然是一个网关。那么全局过滤器肯定是少不了的一个存在。像是鉴权,认证啥的不可能每个服务都做一次,一般都是在网关处就搞定了。
Zuul他就有很强大的过滤器体系来给人使用。
网关当然也不会差这么点东西。
对于SpringCloud体系来说,一切的实现都是那么的简单。那么废话不多说,直接开始写起来。
,
网关内部有一个接口名为GlobalFilter,这个就是网关的全局过滤器接口,只要在应用中实现此接口后注册为春天的Bean,背后就会帮你将这个实现注册到全局过滤器链条里边去。
我这里就简单的写了个模拟鉴权的过滤器实现:

@ component   public  class  AuthFilter  implements  GlobalFilter, Ordered  {   @Override才能   public 才能;Mono< Void>,过滤器(ServerWebExchange 交换,GatewayFilterChain 链),{   ,,,String  token =, exchange.getRequest () .getHeaders () .getFirst (“Authorization");   ,,,//不为空则通过   ,,,if  (! StringUtils.isEmpty(令牌),return  chain.filter(交流);   ,,,ServerHttpResponse  response =, exchange.getResponse ();   ,,,//,封装错误信息   ,,,Map<字符串,Object>, responseData =, Maps.newHashMapWithExpectedSize (3);   ,,,responseData.put (“code",, HttpStatus.UNAUTHORIZED.value ());   ,,,responseData.put (“message",,“Token  is  empty");   ,,,responseData.put (“cause",,“Token  is  empty");   ,,,//,将信息转换为,JSON   ,,,ObjectMapper  ObjectMapper =, new  objectmap ();   ,,,byte [], data =, new 字节[0];   ,,,try  {   ,,,,,data =, objectMapper.writeValueAsBytes (responseData)中;   ,,,},catch  (JsonProcessingException  e), {   ,,,,,e.printStackTrace ();   ,,,}   ,,,//,返回错误信息json   ,,,DataBuffer  buffer =, response.bufferFactory () .wrap(数据);   ,,,response.setStatusCode (HttpStatus.UNAUTHORIZED);   ,,,response.getHeaders阀门()(“Content-Type",,“application/json; charset=UTF-8");   ,,,return  response.writeWith (Mono.just(缓冲));   ,,}//最才能后执行   @Override才能   public 才能;int  getOrder (), {   ,,,return  Ordered.LOWEST_PRECEDENCE;   ,,}   }

虽说是鉴权,但实际上我这就是个简单的演示而已。想知道真正的Spring Security鉴权/认证怎么写?
我以前写的这个:https://github.com/skypyb/code_demo/tree/master/spring-security-demo应该可以帮助你。
,
看我写的这个过滤器内部实现哈,其实就是拿出请求报头中授权的字段(令牌),然后判断是否存在。不存在就返回错误,存在就交给链条中的下一个过滤器。
,
过滤器其实也没啥好说的,那么说说限流。
关于限流这个东西,常见的算法就是漏桶和令牌桶了,对于一个应用单机限流来说也复杂不到哪儿去。
靠着谷歌番石榴包里的RateLimiter工具都能搞定大多数场景了。
不过既然人家网关好心好意给你搞了个限流的实现。那么还是尊重他用一下。
由于网关是用的复述和lua脚本实现了令牌桶的算法,那么先导入几个需要的依赖:

& lt; !——Redis 开始——比;   ,,,& lt; dependency>   ,,,,,& lt; groupId> org.springframework.boot   ,,,,,& lt; artifactId> spring-boot-starter-data-redis   ,,,& lt;/dependency>   ,,,& lt; dependency>   ,,,,,& lt; groupId> org.springframework.boot   ,,,,,& lt; artifactId> spring-boot-starter-data-redis-reactive   ,,,& lt;/dependency>   ,,,& lt; dependency>   ,,,,,& lt; groupId> redis.clients   ,,,,,& lt; artifactId> jedis   ,,,& lt;/dependency>   ,,,& lt; !——Redis 最后,在

既然是复述,那还是先配一下复述,序列化先:

@ configuration   public  class  RedisConfig  {   ,@ bean   public 才能;RedisTemplate<字符串,Object>, redisTemplate (RedisConnectionFactory  connectionFactory), {   ,,,RedisTemplate<字符串,Object>, redisTemplate =, new  RedisTemplate<在();   ,,,StringRedisSerializer  StringRedisSerializer =, new  StringRedisSerializer ();   ,,,redisTemplate.setKeySerializer (stringRedisSerializer);   ,,,redisTemplate.setHashKeySerializer (stringRedisSerializer);   ,,,redisTemplate.setValueSerializer (new  Jackson2JsonRedisSerializer< Object> (Object.class));   ,,,redisTemplate.setHashValueSerializer (new  GenericJackson2JsonRedisSerializer ());   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么为春云网关加上全局过滤器