英文:
Spring Boot controller gets redirected request from Golang app twice
问题
遇到了一个奇怪的问题,
我正在尝试从我的Golang应用程序发送重定向到Spring Boot控制器,然后从Spring Boot控制器返回到Golang应用程序的端点。
这是我用于重定向的Golang代码:
u.counter++ // 初始为0,在每次重定向之前递增
u.logger.Printf("sending redirect number: " + strconv.Itoa(u.counter))
http.Redirect(w, r, "http://myspringboot.app/api/mycontroller?param=" + param + "&counter=" + strconv.Itoa(u.counter), 302)
这是提供/api/mycontroller
端点的Spring Boot控制器:
@GetMapping("/mycontroller")
public void getRedirect(
HttpServletResponse httpServletResponse,
@RequestParam("param") String param,
@RequestParam("counter") String counter
) {
System.out.println("param: " + param + " counter: " + counter);
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
}
我从Golang应用程序发送了5次重定向,因此在Golang端看到了如下的5条日志:
sending redirect number: 1
sending redirect number: 2
sending redirect number: 3
sending redirect number: 4
sending redirect number: 5
而在Spring Boot端,我看到了如下的10条日志:
param: somevalue counter: 1
param: somevalue counter: 2
param: somevalue counter: 1
param: somevalue counter: 2
param: somevalue counter: 3
param: somevalue counter: 3
param: somevalue counter: 4
param: somevalue counter: 4
param: somevalue counter: 5
param: somevalue counter: 5
有趣的是,如果我从Spring Boot控制器中删除以下两行代码:
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
那么控制器会按预期接收到5个重定向请求(而不是10个)。
有人之前遇到过这个问题吗?
Golang应用程序中的/endpoint
与我进行重定向到Spring Boot的HTTP处理程序完全不同。
这两个应用程序都在AWS ECS上运行,每个应用程序分配了一个任务。
Golang应用程序使用https
,Spring Boot应用程序使用http
。我将为Spring Boot应用程序添加ACM证书以检查是否是这个原因。欢迎提供其他想法。
更新:已为Spring Boot添加了ACM证书,问题仍然存在。
更新2:到目前为止,我一直在Chrome上进行测试。在Microsoft Edge上,重复次数有时为2,有时为3。
英文:
Encountered a strange issue,
I'm trying to send redirect from my Golang app to Spring Boot controller, and back from Spring Boot controller to Golang app's endpoint.
Here's my Golang code for redirection:
u.counter++ // initially 0, incremented prior to each redirect
u.logger.Printf("sending redirect number: "+strconv.Itoa(u.counter))
http.Redirect(w, r, "http://myspringboot.app/api/mycontroller?param=" + param + "&counter=" + strconv.Itoa(u.counter), 302)
and this is Spring Boot controller serving /api/mycontroller
endpoint:
@GetMapping("/mycontroller")
public void getRedirect(
HttpServletResponse httpServletResponse,
@RequestParam("param") String param,
@RequestParam("counter") String counter
) {
System.out.println("param: " + param + " counter: " + counter);
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
}
I send 5 redirects from Golang app, hence I see exactly 5 logs as below, on Golang side:
sending redirect number: 1
sending redirect number: 2
sending redirect number: 3
sending redirect number: 4
sending redirect number: 5
and on Spring Boot side, I see 10 logs as below:
param: somevalue counter: 1
param: somevalue counter: 2
param: somevalue counter: 1
param: somevalue counter: 2
param: somevalue counter: 3
param: somevalue counter: 3
param: somevalue counter: 4
param: somevalue counter: 4
param: somevalue counter: 5
param: somevalue counter: 5
And interestingly, if I remove these two lines from Spring Boot controller:
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
then controller receives exactly 5 redirect requests as expected (not 10).
Anyone has seen this issue before?
/endpoint
on Golang app is completely different from http handler in which I'm doing redirection to Spring Boot.
Both apps run on AWS ECS with one task allocated for each app.
Golang app is on https
and Spring Boot is on http
. Going to add ACM certificate for Spring Boot app to check if that's the reason. Any other ideas are appreciated.
UPD. Added ACM certificate to Spring Boot, the issue is still there
UPD2. So far was testing on Chrome. On Microsoft Edge it repeats sometimes 2 and sometimes 3 times
答案1
得分: 0
看起来**/endpoint**在GO端正在重定向回Spring应用程序。因为如这里所提到的,Go的http客户端有10次重定向限制。
所以基本上在GO<->SB之间进行了10次重定向后,GO就停止了。
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
这两行代码意味着将请求重定向到带有302状态的GO。当你删除这部分代码时,你就不会再重定向回"GO",从而打破了循环。
英文:
Looks like /endpoint at GO side is redirecting back to spring app. Because as mentioned here Go http client has 10 redirect limit.
So basically you are redirecting in between GO<->SB and after 10 GO is stopping.
httpServletResponse.setHeader("Location", "https://mygolang.app/endpoint?param=" + param);
httpServletResponse.setStatus(302);
Those 2 lines means redirect the request to GO with 302 status. When you delete this side you are not redirecting back to "GO" so you break the loop.
专注分享java语言的经验与见解,让所有开发者获益!
评论