feign微服务调用携带浏览器信息(header、cookie)

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import java.util.Enumeration;

@Configuration
public class FeignConfiguration {
    @Bean
    public RequestInterceptor requestInterceptor(){
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
                if (requestAttributes != null) {
                    HttpServletRequest request = requestAttributes.getRequest();
                    Enumeration<String> attributeNames = request.getHeaderNames();
                    if (attributeNames != null) {
                        while (attributeNames.hasMoreElements()) {
                            String name = attributeNames.nextElement();
                            String value = request.getHeader(name);
                            requestTemplate.header(name,value);

                        }
                    }
                }
            }
        };
    }
}

 

feign微服务调用携带浏览器信息(header、cookie)

上一篇:网络爬虫第四章


下一篇:设计模式-适配器模式