Go授权头未发送

huangapple go评论66阅读模式
英文:

Go Authorization Header not being sent

问题

解决方案:

问题是我们的请求发送到了Http...而不是https。这意味着Go库将删除授权头,因为它将其视为重定向。

这个问题的更新在底部

我对Go还比较新,目前正在尝试修复我们的一个小服务中的一个错误。
它的基本功能是接收一个需要(MSAL)授权的请求,获取该授权,然后将请求转发到特定的端点。

它是这样做的:

http.HandleFunc("/", ProxyRequest)

func ProxyRequest(wr http.ResponseWriter, req *http.Request) {

	auth, err := tokenHelper.GetAuthorization()
    client := &http.Client{}

    req.URL = newUrl
	req.Host = newUrl.Host
	req.Header.Add("Authorization", *auth)
    resp, err := client.Do(req)
}

这个工作正常,因为打印的头部看起来像这样:Authorization: Bearer eyJ0eXAiOiJ...,其余的头部和正文仍然存在。

如果我将请求复制到Postman中,一切都正常,我的服务器上的调试输出如下所示:

Go授权头未发送

但是,如果我使用Go发送请求,服务器的调试输出如下所示:

Go授权头未发送

唯一的区别是,使用Postman时,它包括授权头部,而使用Go时没有发送。这当然会导致请求被拒绝。

我在互联网和这个网站上搜索了很多,但到目前为止,我似乎找不到我们代码中的错误。任何帮助将不胜感激。

编辑:

所以我尝试了答案中提到的两件事,但我只会发布完整的新请求,因为这是@izca推荐的:

newReq, err := http.NewRequest(req.Method, newUrl.String(), req.Body)

newReq.Header.Add("Authorization", *auth)

log.Println(newReq.Header)
resp, err := client.Do(newReq)

结果完全相同。请求发送到了正确的端点,但是没有授权头部。

Go授权头未发送

日志输出:

map[Authorization:[Bearer eyJ0eXAiOiJKV1QiL....]]
英文:

Solution:

The issue was that our request went to Http... instead of https. This means that the Go library removes the auth header as it treats it as a redirect.

There is an Update to this question at the bottom

So I am kinda new to Go and am currently trying to fix a bug in a small service of ours.
Its basic funtion is to take in a request that needs (MSAL) authorization, getting that authorization and then forwarding the request to the specific endpoint.

It does it like this:

http.HandleFunc("/", ProxyRequest)

func ProxyRequest(wr http.ResponseWriter, req *http.Request) {

	auth, err := tokenHelper.GetAuthorization()
    client := &http.Client{}

    req.URL = newUrl
	req.Host = newUrl.Host
	req.Header.Add("Authorization", *auth)
    resp, err := client.Do(req)
}

This works fine, as in the header if printed looks like this: Authorization: Bearer eyJ0eXAiOiJ... and the rest of the headers and body are still present.

If I copy the request into Postman everything works fine and the debugging Output on my server looks like this:

Go授权头未发送

but if I sent the Request using Go the Servers Debugger Output looks like this:

Go授权头未发送

The only difference being that with Postman it includes the authorization header while with Go it is not being sent. This of course results in the request being rejected.

I have searched the Internet and this site quite a bit but as of yet I can not seem to find the mistake in our code. Any help would be appreciated

Edit:

So I tried both things mentioned in the answers, but I am just going to post the complete new Request one as that is what @izca recommended:

    newReq, err := http.NewRequest(req.Method, newUrl.String(), req.Body)

	newReq.Header.Add("Authorization", *auth)

	log.Println(newReq.Header)
	resp, err := client.Do(newReq)

Which results in exactly the same behaviour. The Request is going to the correct endpoint and is completely barebone, without Authorization Header.

Go授权头未发送

Log Output:

map[Authorization:[Bearer eyJ0eXAiOiJKV1QiL....]]

答案1

得分: 1

req是传入连接的HTTP请求。你不能(也不应该)将其用于传出的HTTP请求。

如果尝试这样做,client.Do()将返回一个错误:

http: Request.RequestURI无法在客户端请求中设置

请使用http.NewRequest()http.NewRequestWithContext()创建一个新的http.Request,并将其用于传出请求,然后将其传递给client.Do()

英文:

req is the HTTP request of the incoming connection. You can't (shouldn't) reuse that to outgoing HTTP requests.

Attempting to do so, client.Do() will return an error:

> http: Request.RequestURI can't be set in client requests

Do create a new http.Request using http.NewRequest() or http.NewRequestWithContext(), and use that for an outgoing request, pass that to client.Do().

答案2

得分: 1

根据@icza的建议,如果你真的想要像反向代理场景中那样转发相同的请求,你可以使用req.Clone(newCtx),它会深拷贝原始请求并返回一个新的请求。

英文:

In extension to what @icza has suggested, if you really want to forward the same request like in the case of reverse proxy scenario, you can use req.Clone(newCtx) which will deep copy the original request and returns a new Request.

huangapple
  • 本文由 发表于 2022年5月16日 19:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/72258859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定