14-15 都是通过配置修改实现的预设过滤器
所谓的全局过滤器 可以认为是全局生效的自定义过滤器
通过新建类实现接口即可
package cn.itcast.study.mygateway;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.List;
@Order(1)
@Component
public class authUserFilter implements GlobalFilter {
String AUTH_USER = "authUser";
String PASS_USER = "dengliang";
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//首先获取请求
ServerHttpRequest request = exchange.getRequest();
//获取请求参数
MultiValueMap<String, String> queryParams = request.getQueryParams();
//判断请求
if(queryParams.containsKey(AUTH_USER)){
List<String> strings = queryParams.get(AUTH_USER);
if(strings.size()==1&&PASS_USER.equals(strings.get(0))) return chain.filter(exchange);
}
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
}
标签:网关,16,exchange,springframework,USER,org,过滤器,import,Gateway
From: https://www.cnblogs.com/dengliang356a/p/17643680.html