package org.security.common.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
public class FeignClientRequestInterceptor implements RequestInterceptor {
	private final String AuthHeader = "blade-Auth";

	@Override
	public void apply(RequestTemplate template) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (null != attributes) {
			HttpServletRequest request = attributes.getRequest();
			if (null != request) {
				String bladeAuth = request.getHeader(AuthHeader);
				if (null != bladeAuth) {
					log.info("<<<<<< Add Header " + AuthHeader + ": " + bladeAuth);
					template.header(AuthHeader, bladeAuth);
				}
			}
		}
	}
}
