package com.lantone.filter;


import com.lantone.common.constant.AuthConstant;
import com.lantone.common.service.RedisService;
import com.lantone.common.util.UUidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

/**
 * @Description:
 * @Author songxl
 * @Date 2021/9/26
 */
@Component
public class GateWayFilter implements WebFilter {
    @Autowired
    private RedisService redisService;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
        //请求头添加uuid
        String uuid = UUidUtil.getUUid();
        String sessionId = exchange.getSession().block().getId();
        ServerHttpRequest request = exchange.getRequest().mutate().header(AuthConstant.UUID,uuid)
                .header(AuthConstant.SESSION_ID,sessionId).build();
        exchange = exchange.mutate().request(request).build();
        redisService.set(AuthConstant.UUID+":"+sessionId,uuid,5);
        return chain.filter(exchange);
    }
}
