我正在尝试使用 Spring Cloud Gateway 重定向到我们的 S3 服务器。问题是重定向时出现错误:
SignatureDoesNotMatch:我们计算出的请求签名与您提供的签名不匹配。检查您的密钥和签名方法。
我在application.yml上有如下配置:
- id: example-s3
predicates:
- Path=/example-s3/**
uri: ${project.services.internal.example-s3.url}
filters:
- RewritePath=/example-s3/(?<segment>.*), /$\{segment}
- PreserveHostHeader
我添加了选项 -PreserveHostHeader 因为它失败了,我看到了这个与 nginx 一起工作的解决方案。它适用于 nginx,但不适用于 Spring Cloud Gateway。
我不知道还能做什么,我已经比较了使用 nginx 和使用 Spring Cloud Gateway 的标头,但我没有看到任何其他可能需要的东西。
如果我添加 - RemoveRequestHeader=Authorization 那么它适用于公共对象,但它在私有时不起作用。所以我认为问题一定出在那个标题中。它是:
Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXX/20230324/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, Signature=fake-signature-to-sctackoverflow
我已经看到它使用标头 host;x-amz-content-sha256;x-amz-date;x-amz-user-agent 进行签名 (SignedHeaders),我验证这些标头是否存在。在 nginx 中,主机标头默认被 Nginx 剥离,所以我在 nginx 中添加的解决方案有效,但在 Spring Cloud Gateway 中我无法使其工作。
我不知道还能尝试或做什么,如何使用 Spring Cloud Gateway 正确反向代理它?
不要使用 RewritePath 过滤器,这将更改 S3 的“主机”。 它可以像这样工作:
- id: example-s3
predicates:
- Path=/**
uri: ${project.services.internal.example-s3.url}
filters:
- PreserveHostHeader