您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何使用Feign傳遞請求頭信息,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
在我之前的文章服務網關Spring Cloud Zuul中,將用戶的登錄id放在了請求頭中傳遞給內部服務。
但是當內部服務之間存在feign調用時,那么請求頭信息會在feign請求的時候傳遞嗎?不會,請求的頭信息和請求參數都不會進行傳遞。
但是我們可以通過通過實現RequestInterceptor接口,完成對所有的Feign請求,傳遞請求頭和請求參數。
import feign.RequestInterceptor; import feign.RequestTemplate; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; /** * Feign請求攔截器(設置請求頭,傳遞登錄信息) * * @author simon * @create 2018-09-07 9:51 **/ public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); requestTemplate.header(name, values); } } } }
這里只設置了請求頭,如果想傳遞請求參數,可以參考如下代碼:
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); requestTemplate.header(name, values); } } Enumeration<String> bodyNames = request.getParameterNames(); StringBuffer body =new StringBuffer(); if (bodyNames != null) { while (bodyNames.hasMoreElements()) { String name = bodyNames.nextElement(); String values = request.getParameter(name); body.append(name).append("=").append(values).append("&"); } } if(body.length()!=0) { body.deleteCharAt(body.length()-1); template.body(body.toString()); logger.info("feign interceptor body:{}",body.toString()); } } }
package com.southgis.ibase.personalConfigure.config; import com.southgis.ibase.utils.FeignBasicAuthRequestInterceptor; import com.southgis.ibase.utils.FeignSpringFormEncoder; import feign.RequestInterceptor; import feign.codec.Encoder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Feign配置注冊(全局) * * @author simon * @create 2018-08-20 11:44 **/ @Configuration public class FeignSupportConfig { /** * feign請求攔截器 * * @return */ @Bean public RequestInterceptor requestInterceptor(){ return new FeignBasicAuthRequestInterceptor(); } }
這個文件放在項目的掃描目錄下,所有的feign調用都會使用此配置。如果只有某個feign調用則可以這樣設置(但配置類不能在掃描目錄下):
@FeignClient(name = "organ",path = "/organ/OrganInfo",configuration = FeignSupportConfig.class)
package com.chitic.module.core.config; import feign.RequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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 FeignConfig { @Bean public RequestInterceptor headerInterceptor() { return template -> { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); if (null != attributes) { HttpServletRequest request = attributes.getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); template.header(name, values); } } } }; } }
需注意,feign調用時不能調用含有HttpServletResponse參數(比如常用的數據導出),以下就不能遠程調用,目前沒找到解決辦法
關于“如何使用Feign傳遞請求頭信息”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。