0%

Feign拦截器

Feign拦截器

通过实现RequestInterceptor接口来实现Feign的拦截器,实现apply方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class FeignRequestInterceptor implements RequestInterceptor
{
@Override
public void apply(RequestTemplate requestTemplate)
{
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();

if (StringUtils.isNotNull(httpServletRequest))
{
Map<String, String> headers = ServletUtils.getHeaders(httpServletRequest);
// 传递用户信息请求头,防止丢失
String userId = headers.get("userId");
if (StringUtils.isNotEmpty(userId))
{
requestTemplate.header("userId", userId);
}
String userName = headers.get("userName");
if (StringUtils.isNotEmpty(userName))
{
requestTemplate.header("userName", userName);
}
String authentication = headers.get("authentication");
if (StringUtils.isNotEmpty(authentication))
{
requestTemplate.header("authentication", authentication);
}

// 配置客户端IP
requestTemplate.header("X-Forwarded-For", IpUtils.getIpAddr(ServletUtils.getRequest()));
}
}
}

欢迎关注我的其它发布渠道