这篇文章将为大家详细讲解有关如何使用Feign传递请求头信息,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。Feign传递请求头信息在我之前的文章服务网关spring cloud Zuul中,将用户的登录i
这篇文章将为大家详细讲解有关如何使用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;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;@Configurationpublic class FeignSupportConfig { @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; @Configurationpublic 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传递请求头信息”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
--结束END--
本文标题: 如何使用Feign传递请求头信息
本文链接: https://lsjlt.com/news/324144.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0