1.学习要点
2.编码实现
package com.atguigu.springcloud.filter;
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.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import java.util.Date;
/**
* 自定义的网关全局过滤器,这里是举一个例子,师傅领进门,修行靠个人
*/
@Component
public class MyLogGatewayFilter implements GlobalFilter, Ordered
{
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
{
System.out.println("***********come in MyLogGatewayFilter: "+ new Date());
String uname = exchange.getRequest().getQueryParams().getFirst("uname");
if(uname == null)
{
System.out.println("用户名为null,非法用户.......");
exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
return exchange.getResponse().setComplete();
}
// 放行
return chain.filter(exchange);
}
@Override
public int getOrder()
{
// 返回值越小,优先级越高
return 0;
}
}
3.打开浏览器进行测试
输入:http://localhost:9527/payment/get/1
输出:
输入:http://localhost:9527/payment/get/1?uname=111
测试通过
版权声明:本文为LOL111222333原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。