OpenFeign調(diào)用服務(wù)請(qǐng)求頭丟失Token的解決
OpenFeign調(diào)用服務(wù)請(qǐng)求頭丟失Token
導(dǎo)致原因:

解決方案:

代碼實(shí)現(xiàn)
@Configuration
@Slf4j
public class FeignConfig {
@Value("${jwt.header}")
private String tokenHeader;
@Bean("requestInterceptor")
public RequestInterceptor requestInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
log.info("進(jìn)入feign攔截器...");
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();// 老請(qǐng)求
String authorization = request.getHeader(tokenHeader);
log.info(authorization);
requestTemplate.header(tokenHeader, authorization);
}
};
}
}
Feign傳參對(duì)象數(shù)據(jù)丟失問(wèn)題
Feigin不支持Key-value形式的請(qǐng)求體傳參,所有在傳遞對(duì)象參數(shù)的時(shí)候需要將服務(wù)端的接口加上@RequstBody注解,F(xiàn)eign消費(fèi)端也需要加上@RequstBody,但是會(huì)出現(xiàn)前端在直接訪問(wèn)服務(wù)器接口時(shí),需要構(gòu)建JSON串放在Body里傳遞過(guò)來(lái)。
Get請(qǐng)求又不支持Body。為了解決這個(gè)問(wèn)題,這里記錄解決方案。
1.如果不考慮前端直接調(diào)用接口和Feign調(diào)用接口不一致
服務(wù)端的接口加上@RequstBody注解,F(xiàn)eign消費(fèi)端也需要加上@RequstBody。
2.升級(jí)springboot版本到2.1.x.使用Spring Cloud OpenFeign提供@SpringQueryMap注解
Feign里加上
class AuditFeiginConfig {
@Bean
public Contract customerContract() {
return new feign.Contract.Default();
}
}
完整Feign代碼:
import feign.Contract;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.fasterxml.jackson.databind.JsonNode;
import com.galaplat.base.core.common.exception.BaseException;
import com.galaplat.product.center.databook.plugin.vos.AuditVO;
@FeignClient(name = "galaplat-product-center", fallback = AuditFeignHystrixService.class,configuration = IAuditFeign.AuditFeiginConfig.class)
public interface IAuditFeign {
@PostMapping("/XXX")
JsonNode submit(@SpringQueryMap AuditVO auditVO) throws Exception;
class AuditFeiginConfig {
@Bean
public Contract customerContract() {
return new feign.Contract.Default();
}
}
}
這樣服務(wù)端接口就不一定要加@RequstBody.
服務(wù)端接口代碼
//這里就可以不加@RequestBody,默認(rèn)應(yīng)該是@RequestParam
@PostMapping("/XXX")
public Object submit(AuditVO auditVO) throws Exception {
return null;
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot整合Mybatis-plus案例及用法實(shí)例
mybatis-plus是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合Mybatis-plus案例及用法實(shí)例的相關(guān)資料,需要的朋友可以參考下2022-11-11
springboot中request和response的加解密實(shí)現(xiàn)代碼
這篇文章主要介紹了springboot中request和response的加解密實(shí)現(xiàn),在springboot中提供了RequestBodyAdviceAdapter和ResponseBodyAdvice,利用這兩個(gè)工具可以非常方便的對(duì)請(qǐng)求和響應(yīng)進(jìn)行預(yù)處理,需要的朋友可以參考下2022-06-06
解決IDEA中快捷鍵Alt+Enter不能使用的問(wèn)題
這篇文章主要介紹了解決IDEA中快捷鍵Alt+Enter不能使用的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
由ArrayList來(lái)深入理解Java中的fail-fast機(jī)制
fail-fast俗稱(chēng)快速失敗,是在多線程進(jìn)行迭代操作時(shí)產(chǎn)生沖突的一種異常拋出機(jī)制,下面我們就由ArrayList來(lái)深入理解Java中的fail-fast機(jī)制.2016-05-05
Myeclipse部署Tomcat_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章給大家介紹了Myeclipse部署Tomcat的相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07

