package com.pj.more;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import cn.dev33.satoken.id.SaIdUtil;
import reactor.core.publisher.Mono;

/**
 * 全局过滤器，为请求添加 Id-Token 
 * 
 * @author kong
 *
 */
@Component
public class SaIdTokenFilter implements GlobalFilter {
	
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String token=SaIdUtil.getToken();
        System.out.println(token+"---------header-----");
        ServerHttpRequest newRequest = exchange
                .getRequest()
                .mutate()
                // 为请求追加 Id-Token 参数 
                .header(SaIdUtil.ID_TOKEN,token)
                .build();
        ServerWebExchange newExchange = exchange.mutate().request(newRequest).build();
        return chain.filter(newExchange);
    }
    
}