使用Feign传递请求头信息(Finchley版本)

网友投稿 670 2022-10-24

使用Feign传递请求头信息(Finchley版本)

使用Feign传递请求头信息(Finchley版本)

目录Feign传递请求头信息实现RequestInterceptor接口注册配置Feign调用微服务传递header请求头

Feign传递请求头信息

在我之前的文章服务网关Spring Cloud Zuul中,将用户的登录id放在了请求头中传递给内部服务。

但是当内部服务之间存在feign调用时,那么请求头信息会在feign请求的时候传递吗?不会,请求的头信息和请求参数都不会进行传递。

但是我们可以通过通过实现RequestInterceptor接口,完成对所有的Feign请求,传递请求头和请求参数。

实现RequestInterceptor接口

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 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 headerNames = request.getHeaderNames();

if (headerNames != null) {

while (headerNames.hasMoreElements()) {

String name = headerNames.nextElement();

String values = request.getHeader(name);

requestTemplate.header(name, values);

}

}

Enumeration bodyNames = request.getParameterNames();

http://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)

Feign调用微服务传递header请求头

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 headerNames = request.getHeaderNames();

if (headerNames != null) {

while (headerNames.hasMoreElements()) {

String name = headerNames.nextElement();

String values = request.getHeader(name);

template.header(name, values);

}

}

}

};

}

}

需注意,feign调用时不能调用含有HttpServletResponse参数(比如常用的数据导出),以下就不能远程调用,目前没找到解决办法

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Orbited- 浏览器间的实时通讯框架
下一篇:python信息标记与信息提取
相关文章

 发表评论

暂时没有评论,来抢沙发吧~