package com.zd.common.security.feign;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Component;
import com.zd.common.core.constant.SecurityConstants;
import com.zd.common.core.utils.ServletUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.utils.ip.IpUtils;
import feign.RequestInterceptor;
import feign.RequestTemplate;

import static com.zd.common.core.constant.SecurityConstants.DB;

/**
 * feign 请求拦截器
 *
 * @author zd
 */
@Component
public class FeignRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest httpServletRequest = ServletUtils.getRequest();
        if (StringUtils.isNotNull(httpServletRequest)) {
            Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
            // 传递用户信息请求头，防止丢失
            String userId = headers.get(SecurityConstants.DETAILS_USER_ID);
            if (StringUtils.isNotEmpty(userId)) {
                requestTemplate.header(SecurityConstants.DETAILS_USER_ID, userId);
            }
            //配置多数据源情况下对feign请求的兼容
            String dbName = headers.get(DB);
            if (StringUtils.isNotEmpty(dbName)) {
                requestTemplate.header(SecurityConstants.DB, dbName);
            }

            String userName = headers.get(SecurityConstants.DETAILS_USERNAME);
            if (StringUtils.isNotEmpty(userName)) {
                requestTemplate.header(SecurityConstants.DETAILS_USERNAME, userName);
            }
            String authentication = headers.get(SecurityConstants.AUTHORIZATION_HEADER);
            if (StringUtils.isNotEmpty(authentication)) {
                requestTemplate.header(SecurityConstants.AUTHORIZATION_HEADER, authentication);
            }

            // 配置客户端IP
            requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr(ServletUtils.getRequest()));
        }
    }
}
