广告
返回顶部
首页 > 资讯 > 后端开发 > Python >OpenFeign调用服务请求头丢失Token的解决
  • 899
分享到

OpenFeign调用服务请求头丢失Token的解决

2024-04-02 19:04:59 899人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

OpenFeign调用服务请求头丢失Token 导致原因: 解决方案: 代码实现 @Configuration @Slf4j public class FeignConfig

OpenFeign调用服务请求头丢失Token

导致原因:

在这里插入图片描述

解决方案:

在这里插入图片描述

代码实现


@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("进入feign拦截器...");
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                httpservletRequest request = requestAttributes.getRequest();// 老请求
                String authorization = request.getHeader(tokenHeader);
                log.info(authorization);
                requestTemplate.header(tokenHeader, authorization);
            }
        };
    }
}

Feign传参对象数据丢失问题

Feigin不支持Key-value形式的请求体传参,所有在传递对象参数的时候需要将服务端的接口加上@RequstBody注解,Feign消费端也需要加上@RequstBody,但是会出现前端在直接访问服务器接口时,需要构建JSON串放在Body里传递过来。

Get请求又不支持Body。为了解决这个问题,这里记录解决方案。

1.如果不考虑前端直接调用接口和Feign调用接口不一致

服务端的接口加上@RequstBody注解,Feign消费端也需要加上@RequstBody。

2.升级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();
        }
    }
}

这样服务端接口就不一定要加@RequstBody.

服务端接口代码


 //这里就可以不加@RequestBody,默认应该是@RequestParam
    @PostMapping("/XXX")
 public Object submit(AuditVO auditVO) throws Exception {
  return null;
 }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: OpenFeign调用服务请求头丢失Token的解决

本文链接: https://www.lsjlt.com/news/129110.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作