header设置透传

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
    private static final String CONTENT_LENGTH_KEY = "content-length";
    private static final String TRANSFER_ENCODING_KEY="Transfer-Encoding";
    private static final String TRANSFER_ENCODING_VALUE="chunked";

    @Override
    public void apply(RequestTemplate template) {
        HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
        Enumeration<String> headerNames = request.getHeaderNames();
        if(headerNames !=null){
            while (headerNames.hasMoreElements()) {
                String headName = headerNames.nextElement();
                //跳过 content-length
                if(!CONTENT_LENGTH_KEY.equals(headName)){
                    template.header(headName, request.getHeader(headName));
                }
            }
            //设置token
            Object userToken = request.getAttribute(CONST_USER_NAME_TOKEN);
            if (userToken != null) {
                template.header(CONST_USER_NAME_TOKEN, userToken.toString());
            }

            //shortName设置
            Object shortName = request.getAttribute(SHORT_NAME);
            if (shortName != null) {
                template.header(SHORT_NAME, shortName.toString());
            }

            //设置TRANSFER_ENCODING 忽略 content-length
            template.header(TRANSFER_ENCODING_KEY,TRANSFER_ENCODING_VALUE);
        }
    }

}

 

posted on 2023-04-03 16:58  张释文  阅读(197)  评论(0编辑  收藏  举报