57b0e96e6195dd180b23508f56379e61592bff45.svn-base 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package org.jeecg.config;
  2. import org.jeecg.filter.GlobalAccessTokenFilter;
  3. import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. import reactor.core.publisher.Mono;
  8. /**
  9. * @author scott
  10. * @date 2020/5/26
  11. * 路由限流配置
  12. */
  13. @Configuration
  14. public class RateLimiterConfiguration {
  15. /**
  16. * IP限流 (通过exchange对象可以获取到请求信息,这边用了HostName)
  17. */
  18. @Bean
  19. @Primary
  20. public KeyResolver ipKeyResolver() {
  21. return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
  22. }
  23. /**
  24. * 用户限流 (通过exchange对象可以获取到请求信息,获取当前请求的用户 TOKEN)
  25. */
  26. @Bean
  27. public KeyResolver userKeyResolver() {
  28. //使用这种方式限流,请求Header中必须携带X-Access-Token参数
  29. return exchange -> Mono.just(exchange.getRequest().getHeaders().getFirst(GlobalAccessTokenFilter.X_ACCESS_TOKEN));
  30. }
  31. /**
  32. * 接口限流 (获取请求地址的uri作为限流key)
  33. */
  34. @Bean
  35. public KeyResolver apiKeyResolver() {
  36. return exchange -> Mono.just(exchange.getRequest().getPath().value());
  37. }
  38. }