在Go中进行基本的HTTP身份验证

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

Basic HTTP Auth in Go

问题

我正在尝试使用下面的代码进行基本的HTTP身份验证,但是它抛出了以下错误:

> 2013/05/21 10:22:58 Get mydomain.example: 不支持的协议方案 ""
> 退出状态 1

func basicAuth() string {
    var username string = "foo"
    var passwd string = "bar"
    client := &http.Client{}
    req, err := http.NewRequest("GET", "mydomain.example", nil)
    req.SetBasicAuth(username, passwd)
    resp, err := client.Do(req)
    if err != nil{
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s
}

有任何想法我可能做错了什么吗?

英文:

I'm trying to do basic HTTP auth with the code below, but it is throwing out the following error:

> 2013/05/21 10:22:58 Get mydomain.example: unsupported protocol scheme ""
> exit status 1

func basicAuth() string {
    var username string = "foo"
    var passwd string = "bar"
    client := &http.Client{}
    req, err := http.NewRequest("GET", "mydomain.example", nil)
    req.SetBasicAuth(username, passwd)
    resp, err := client.Do(req)
    if err != nil{
        log.Fatal(err)
    }
    bodyText, err := ioutil.ReadAll(resp.Body)
    s := string(bodyText)
    return s
}

Any idea what I may be doing wrong?

答案1

得分: 111

潜在的问题是,如果您的网站进行任何重定向... Go语言将在重定向时删除您指定的标头。 (我不得不使用wireshark来查看这个!您可以通过右键单击然后“检查元素”并单击网络选项卡在Chrome中快速找到它)

您需要定义一个重定向函数,将标头添加回去。

func basicAuth(username, password string) string {
  auth := username + ":" + password
  return base64.StdEncoding.EncodeToString([]byte(auth))
}

func redirectPolicyFunc(req *http.Request, via []*http.Request) error{
  req.Header.Add("Authorization","Basic " + basicAuth("username1","password123"))
  return nil
}

func main() {
  client := &http.Client{
    Jar: cookieJar,
    CheckRedirect: redirectPolicyFunc,
  }

  req, err := http.NewRequest("GET", "http://localhost/", nil)
  req.Header.Add("Authorization","Basic " + basicAuth("username1","password123")) 
  resp, err := client.Do(req)
}
英文:

the potential 'gotcha' is if your website does any redirects... Go-lang will drop your specified headers on the redirects. (I had to do wireshark to see this! You can quicky find out in chrome by right-clicking then "inspect element" and click network tab)

you'll want to define a redirect function that adds the header back in.

func basicAuth(username, password string) string {
  auth := username + ":" + password
  return base64.StdEncoding.EncodeToString([]byte(auth))
}

func redirectPolicyFunc(req *http.Request, via []*http.Request) error{
  req.Header.Add("Authorization","Basic " + basicAuth("username1","password123"))
  return nil
}

func main() {
  client := &http.Client{
    Jar: cookieJar,
    CheckRedirect: redirectPolicyFunc,
  }

  req, err := http.NewRequest("GET", "http://localhost/", nil)
  req.Header.Add("Authorization","Basic " + basicAuth("username1","password123")) 
  resp, err := client.Do(req)
}

答案2

得分: 68

你需要为NewRequest指定协议,例如"http://",请参考这里

req, err := http.NewRequest("GET", "http://mydomain.example", nil)
英文:

You need to specify the protocol for NewRequest, e.g. "http://", see here.

req, err := http.NewRequest("GET", "http://mydomain.example", nil)

1: http://play.golang.org/p/bX0r-PbCv2 "here"

huangapple
  • 本文由 发表于 2013年5月21日 23:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/16673766.html
匿名

发表评论

匿名网友

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

确定