上面配置文件内容意思是访问的路径 :8000/all/routeAll (网关地址/all/routeAll)截取 /all 部分,会被转发到 (uri/routeAll),并传入 'pass=yes' 参数(注意为 Get 请求)
启动注册中心工程(registry-service),网关工程(master-service)和服务工程 A/B(a-service、b-service)
项目启动成功后访问::8000/all/routeAll
首先会返回 'I'm Spring Cloud Gateway fallback.',因为服务还未被注册到注册中心
然后会返回 '{"msg":"缺少凭证","code":-1}',因为配置了全局过滤器,如下
package com.zwc.gateway.config.filters; import com.alibaba.fastjson.JSONObject; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import org.springframework.core.io.buffer.DataBuffer; import java.net.InetSocketAddress; import java.nio.charset.StandardCharsets; /** * @ClassName TokenFilter * @Desc TODO 请求认证过滤器 * @Date 2019/6/29 17:49 * @Version 1.0 */ public class TokenFilter implements GlobalFilter{ @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 请求对象 ServerHttpRequest request = exchange.getRequest(); // 响应对象 ServerHttpResponse response = exchange.getResponse(); // 只有综合路由才添加这个全局过滤器(routesId:route_all) // 如果请求路径中不存在 routeAll 字符串 if(request.getURI().toString().indexOf("routeAll") == -1){ System.out.println("filter -> return"); // 直接跳出 return chain.filter(exchange); } // 从请求中获取 token 参数 String token = exchange.getRequest().getQueryParams().getFirst("token"); // 如果为空,那么将返回 401 if (token == null || token.isEmpty()) { // 响应消息内容对象 JSONObject message = new JSONObject(); // 响应状态 message.put("code", -1); // 响应内容 message.put("msg", "缺少凭证"); // 转换响应消息内容对象为字节 byte[] bits = message.toJSONString().getBytes(StandardCharsets.UTF_8); DataBuffer buffer = response.bufferFactory().wrap(bits); // 设置响应对象状态码 401 response.setStatusCode(HttpStatus.UNAUTHORIZED); // 设置响应对象内容并且指定编码,否则在浏览器中会中文乱码 response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8"); // 返回响应对象 return response.writeWith(Mono.just(buffer)); } // 获取请求地址 String beforePath = request.getPath().pathWithinApplication().value(); // 获取响应状态码 HttpStatus beforeStatusCode = response.getStatusCode(); System.out.println("响应码:" + beforeStatusCode + ",请求路径:" + beforePath); // 请求前 System.out.println("filter -> before"); // 如果不为空,就通过 return chain.filter(exchange).then(Mono.fromRunnable(() -> { // 获取请求地址 String afterPath = request.getPath().pathWithinApplication().value(); // 获取响应状态码 HttpStatus afterStatusCode = response.getStatusCode(); System.out.println("响应码:" + afterStatusCode + ",请求路径:" + afterPath); // 响应后 System.out.println("filter -> after"); })); } }