https://gitee.com/zjj19941/ZJJ_Neaten5.10/tree/master/ZJJ_Gateway/demo08
自定义token校验过滤器代码实现import lombok.extern.slf4j.Slf4j; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Optional; @Component @Order(-1) @Slf4j public class CheckAuthFilter implements GlobalFilter { @Override public Mono测试filter(ServerWebExchange exchange, GatewayFilterChain chain) { //校验请求头中的token List token = exchange.getRequest().getHeaders().get("token"); log.info("token的值是:" + token); if (token.isEmpty()) { return chain.filter(exchange); } // TODO token校验 return chain.filter(exchange); } }
启动下面两个服务:
MallOrderApplication
MallGatewayApplication
然后postman发起请求
http://localhost:8888/order/demo01
控制台输出:
此时就打印出来你访问接口的时候header里面的token的值了, 你就可以利用这个token的值去进行校验token是否合法.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)