package com.huimv.gateway.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
// 注释该注解，过滤器就时效
//@Component
public class TokenFilter implements GlobalFilter, Ordered {
    Logger logger = LoggerFactory.getLogger(TokenFilter.class);

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        logger.info("========> 开始进行转发前过滤工作 ========>");
        ServerHttpRequest request = exchange.getRequest();
        logger.info("Path:"+request.getPath().value());
        logger.info("HostName:"+request.getLocalAddress().getHostName());
        logger.info("Port:"+request.getLocalAddress().getPort());
        logger.info("Address:"+request.getLocalAddress().getAddress());
        logger.info("QueryParams:"+request.getQueryParams().toString());
        logger.info("URI:"+request.getURI().toString());
        logger.info("<======== 转发前结束过滤工作 <========");
//
//        logger.info("StatusCode:"+exchange.getResponse().getStatusCode().toString());

        // --过滤token--
//        String token = exchange.getRequest().getQueryParams().getFirst("token");
//        if (token == null || token.isEmpty()) {
//            logger.info("StatusCode:"+exchange.getResponse().getStatusCode().toString());
//            logger.info("token is empty...");
//            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//            return exchange.getResponse().setComplete();
//        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}