package com.kcim.common.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * @program: CostAccount
 * @description:
 * @author: Wang.YS
 * @create: 2023-09-19 17:16
 **/
@Configuration
public class FeignConfiguration implements RequestInterceptor {
    /**
     * Called for every request. Add data using methods on the supplied {@link RequestTemplate}.
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
        RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();


//        获取请求体
        javax.servlet.http.HttpServletRequest request = attributes.getRequest();
//        获取token
        String token = request.getHeader("token");
//        注入feign的请求头
        template.header("token",token);

    }

}
