Spring Cloud Feign請求添加headers的實現(xiàn)方式
背景
部門技術(shù)升級,HttpClient需要升級Feign調(diào)用,重構(gòu)某一個資方時遇到請求需要添加上自定義簽名headers,踩坑后記錄了下來
方案一
方法上的@RequestMapping注解添加headers信息
@RequestMapping注解的屬性中包含一個headers數(shù)組,所以嘗試使用,在指定的方法上@RequestMapping注解中添加需要的headers,可以是寫死的,也可以讀取配置,測試是有效的
同理@RequestMapping一組的@PostMapping,@GetMapping注解等均適用
@FeignClient(name = "server",url = "127.0.0.1:8080") public interface FeignTest { ? ? @RequestMapping(value = "/test",headers = {"app=test-app","token=${test-app.token}"}) ? ? String test(); }
方案二
接口上的@RequestMapping注解添加headers信息
針對單個方法可以在方法上的@RequestMapping注解中添加headers,如果同一個接口中所有的方法都需要同樣的headers時在方法上加就比較繁瑣了,可以在接口上的@RequestMapping注解中添加headers,使整個接口的方法均被添加同樣的headers
@FeignClient(name = "server",url = "127.0.0.1:8080") @RequestMapping(value = "/",headers = {"app=test-app","token=${test-app.token}"}) public interface FeignTest { ? ? @RequestMapping(value = "/test") ? ? String test(); }
方案三
使用@Headers注解添加headers信息
@FeignClient(name = "server",url = "127.0.0.1:8080") @Headers({"app: test-app","token: ${test-app.token}"}) public interface FeignTest { ? ? @RequestMapping(value = "/test") ? ? String test(); }
查看openfeign官方文檔發(fā)現(xiàn)其使用的是@Headers來添加headers,測試發(fā)現(xiàn)并沒有生效,spring cloud使用了自己的SpringMvcContract來解析注解,所以需要自己實現(xiàn)一個Contract來實現(xiàn)對@Headers注解的支持,具體實現(xiàn)參照http://www.dbjr.com.cn/article/282530.htm
方案四
自定義RequestInterceptor添加headers信息
feign提供了一個攔截器接口RequestInterceptor,實現(xiàn)RequestInterceptor接口就可以實現(xiàn)對feign請求的攔截,接口提供了一個方法apply(),實現(xiàn)apply()方法
@Component public class FeignRequestInterceptor implements RequestInterceptor { ? ? @Value("${test-app.token}") ? ? private String token; ? ?? ? ? @Override ? ? public void apply(RequestTemplate requestTemplate) { ? ? ? ? requestTemplate.header("app","test-app");//靜態(tài) ? ? ? ? requestTemplate.header("token",token);//讀配置 ? ? } }
實現(xiàn)apply()方法直接添加header會攔截所有的請求都加上headers,如果不是所有的feign請求都需要用到不建議此方法
方案五
自定義RequestInterceptor實現(xiàn)添加動態(tài)數(shù)據(jù)到header
以上方案都不適合把動態(tài)的數(shù)據(jù)放入headers中,而通常場景下可能經(jīng)常需要把計算的簽名,用戶id等動態(tài)信息設(shè)置到headers,所以還需要一個更加完善的方案。
方案1/2/3均不能設(shè)置動態(tài)的值,方案4可以設(shè)置動態(tài)值,但是沒做到請求的區(qū)分,所以在方案4的基礎(chǔ)上進行改進得到了較為完善的方案5。
具體實現(xiàn)如下:
在請求調(diào)用代碼中,獲取到HttpServletRequest對象,將需要添加到headers中的值封裝成一個map后放入HttpServletRequest的attribute域中
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); String signedMsg = getSignedMsg(reqJson); // 計算簽名字符串 Map<String, String> reqMap = new HashMap<>(); reqMap.put("content-type", "application/json");//常量字段 reqMap.put("accessKey", accessKey);//常量字段 reqMap.put("signedMsg", signedMsg);//動態(tài)計算/獲取字段 request.setAttribute("customizedRequestHeader", reqMap);
在自定義RequestInterceptor中獲取到HttpServletRequest對象的attribute域中指定的key,將key對應(yīng)map中的所有參數(shù)加入到headers。
@Component public class FeignRequestInterceptor implements RequestInterceptor { ? ? @Override ? ? public void apply(RequestTemplate requestTemplate) { ? ? ? ? ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); ? ? ? ? HttpServletRequest request = attributes.getRequest(); ? ? ? ? // 設(shè)置自定義header ? ? ? ? // 設(shè)置request中的attribute到header以便轉(zhuǎn)發(fā)到Feign調(diào)用的服務(wù) ? ? ? ? Enumeration<String> reqAttrbuteNames = request.getAttributeNames(); ? ? ? ? if (reqAttrbuteNames != null) { ? ? ? ? ? ? while (reqAttrbuteNames.hasMoreElements()) { ? ? ? ? ? ? ? ? String attrName = reqAttrbuteNames.nextElement(); ? ? ? ? ? ? ? ? if (!"customizedRequestHeader".equalsIgnoreCase(attrName)) { ? ? ? ? ? ? ? ? ? ? continue; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? Map<String,String> requestHeaderMap = (Map)request.getAttribute(attrName); ? ? ? ? ? ? ? ? for (Map.Entry<String, String> entry : requestHeaderMap.entrySet()) { ? ? ? ? ? ? ? ? ? ? requestTemplate.header(entry.getKey(), entry.getValue()); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? } ? ? } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java動態(tài)規(guī)劃之硬幣找零問題實現(xiàn)代碼
這篇文章主要介紹了Java動態(tài)規(guī)劃之硬幣找零問題實現(xiàn)代碼,具有一定參考價值,需要的朋友可以了解下。2017-11-11避免sql注入_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了避免sql注入,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08JAVAWEB實現(xiàn)簡單的商城項目(一)實例代碼解析
本文給大家分享一段實例代碼給大家介紹JAVAWEB實現(xiàn)簡單的商城項目(一),非常具有參考價值,感興趣的朋友一起學(xué)習(xí)吧2016-02-02