使用 Feign 客户端从一个服务调用 API 到另一个服务时访问被拒绝

问题描述 投票:0回答:1

我们在 AWS EKS 上将两个微服务部署到同一命名空间中。我们正在尝试将一项服务的 API 调用到另一项服务中。

如果我们尝试使用curl从POD调用API,它可以工作,如果我们也在代码中使用Web客户端来调用API,那么它可以工作,但是当我们使用Feign客户端时,它不工作。

在所有实现中仅传递授权和内容类型标头。

错误如下:

<FONT face=\"Helvetica\">
Your system policy has denied access to the requested URL.
</FONT>
<FONT face=\"Helvetica\">
Transaction ID: dcff724f84f5aca8-00000000eadbc942-0000000065e5d403
</FONT>
<FONT face=\"Helvetica\" SIZE=2>
For assistance, contact your network support team.
</FONT>

feign.FeignException$Forbidden: [403 Forbidden] during [POST] to [http://test-service:8080/process/7336f66a-7ab5-4a23-23ed-d7cde05a4eda]
<TITLE>Access Denied</TITLE>

使用 弹簧启动:3.1.6 spring-cloud-starter-openfeign:4.0.2

我正在尝试从部署在 AWS EKS 的同一命名空间中的另一服务调用一个服务 API。

使用 Web 客户端我可以成功调用 API,但在使用 Feign 客户端时收到 403。在两个实现中传递相同的标头。

java spring-boot amazon-eks spring-cloud-feign openfeign
1个回答
0
投票

你的 feign 无法将你的 Authorization header 发送到下一个 api,你必须在你的代码中使用 feign 拦截器,如下所示

@Component
  public class FeignRequestInterceptor implements 
  RequestInterceptor 
{

    @Override
    public void apply(RequestTemplate template) {
        final RequestAttributes requestAttributes = 
        RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
        final HttpServletRequest httpServletRequest = 
        ((ServletRequestAttributes) 
          requestAttributes).getRequest();
        template.header(HttpHeaders.AUTHORIZATION, 
        httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
    }
}

在你的项目配置包中添加该类,它会自动将Authorization header传递给下一个api

© www.soinside.com 2019 - 2024. All rights reserved.