PS:

response.setComplete();// 加上这个会使得前端取不到返回值,其content-length = 0

  1. package xyz.haishui.offer.gateway.filter;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  6. import org.springframework.cloud.gateway.filter.GlobalFilter;
  7. import org.springframework.core.Ordered;
  8. import org.springframework.core.io.buffer.DataBuffer;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.server.reactive.ServerHttpResponse;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.web.server.ServerWebExchange;
  14. import reactor.core.publisher.Mono;
  15. import xyz.haishui.offer.common.RestResult;
  16. import xyz.haishui.offer.gateway.config.AuthFilterConfig;
  17. import java.net.URI;
  18. import java.nio.charset.StandardCharsets;
  19. import java.util.List;
  20. @Component
  21. public class AuthFilter implements GlobalFilter, Ordered {
  22. private Logger logger = LoggerFactory.getLogger(this.getClass());
  23. @Autowired
  24. private AuthFilterConfig authFilterConfig;
  25. @Override
  26. public int getOrder() {
  27. return 1;
  28. }
  29. /**
  30. * Process the Web request and (optionally) delegate to the next {@code WebFilter}
  31. * through the given {@link GatewayFilterChain}.
  32. *
  33. * @param exchange the current server exchange
  34. * @param chain provides a way to delegate to the next filter
  35. * @return {@code Mono<Void>} to indicate when request processing is complete
  36. */
  37. @Override
  38. public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
  39. if (!authFilterConfig.isEnable()){
  40. return chain.filter(exchange);
  41. }
  42. URI uri = exchange.getRequest().getURI();
  43. if (authFilterConfig.getCheckUrls().stream().anyMatch(
  44. url -> uri.getPath().matches(url)
  45. )){
  46. // 检查token
  47. List<String> authorizationList = exchange.getRequest().getHeaders().get("Authorization");
  48. if (authorizationList==null||authorizationList.size()==0){
  49. return errorReturn(exchange.getResponse());
  50. }
  51. logger.debug("检查token");
  52. }
  53. return chain.filter(exchange);
  54. }
  55. /**
  56. * token检验不通过
  57. * @param response ServerHttpResponse
  58. * @return 写入错误提示并结束过滤链条,返回给前端
  59. */
  60. private Mono<Void> errorReturn(ServerHttpResponse response){
  61. String data=new RestResult<String>().unauthorized().msg("请登录").toString();
  62. byte[] bytes = data.getBytes(StandardCharsets.UTF_8);
  63. DataBuffer buffer = response.bufferFactory().wrap(bytes);
  64. response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
  65. // response.setComplete();// 加上这个会使得前端取不到返回值,其content-length = 0
  66. response.setStatusCode(HttpStatus.OK);
  67. return response.writeWith(Mono.just(buffer));
  68. }
  69. }