package com.yeejoin.amos.boot.biz.common.feign;

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

import javax.servlet.http.HttpServletRequest;

/**
 * @description:
 * @author: tw
 * @createDate: 2023/8/1
 */
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
    public FeignBasicAuthRequestInterceptor() {

    }

    @Override
    public void apply(RequestTemplate template) {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null) {
            return;
        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = attributes.getRequest();

        //设置header
        String token = request.getHeader("token");
        if (token == null) {
            token = request.getHeader("X-Access-Token");
        }
        String product = request.getHeader("product");
        String appKey = request.getHeader("appKey");
        template.header("token", token);
        template.header("product", product);
        template.header("appKey", appKey);
    }

}
