亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

向基于 Go 的反向代理服務器添加基本身份驗證

向基于 Go 的反向代理服務器添加基本身份驗證

Go
滄海一幻覺 2022-07-18 17:12:16
我想使用 Go 反向代理服務器保護 Docker 守護程序 REST API。我發現這篇文章非常相關。我從未使用過 Go,因此不確定如何使用靜態用戶名和密碼對此進行基本身份驗證。我嘗試了我碰巧在谷歌上找到的所有可能的方法,但沒有一個對我有用。有人可以幫助將靜態 basicAuth 身份驗證添加到以下代碼,以便請求只有在請求包含用戶名和密碼時才能訪問 Docker 守護程序 API: https ://github.com/ben-lab/blog-material/blob/master/ golang-reverse-proxy-2/reverse-proxy.gopackage mainimport (    "fmt"    "io"    "log"    "net/http"    "time"    "github.com/tv42/httpunix")func handleHTTP(w http.ResponseWriter, req *http.Request) {    fmt.Printf("Requested : %s\n", req.URL.Path)    u := &httpunix.Transport{        DialTimeout:           100 * time.Millisecond,        RequestTimeout:        1 * time.Second,        ResponseHeaderTimeout: 1 * time.Second,    }    u.RegisterLocation("docker-socket", "/var/run/docker.sock")    req.URL.Scheme = "http+unix"    req.URL.Host = "docker-socket"    resp, err := u.RoundTrip(req)    if err != nil {        http.Error(w, err.Error(), http.StatusServiceUnavailable)        return    }    defer resp.Body.Close()    copyHeader(w.Header(), resp.Header)    w.WriteHeader(resp.StatusCode)    io.Copy(w, resp.Body)}func copyHeader(dst, src http.Header) {    for k, vv := range src {        for _, v := range vv {            dst.Add(k, v)        }    }}func main() {    server := &http.Server{        Addr:    ":8888",        Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handleHTTP(w, r) }),    }    log.Fatal(server.ListenAndServe())}https://github.com/ben-lab/blog-material/blob/master/golang-reverse-proxy-2/reverse-proxy.go
查看完整描述

2 回答

?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

您可以通過調用 BasicAuth()您的


req *http.Request object

喜歡:


user, pass, _ := req.BasicAuth()

然后將 user 和 pass 與您擁有的靜態值進行比較。


https://golang.org/pkg/net/http/#Request.BasicAuth


更新:


func handleHTTP(w http.ResponseWriter, req *http.Request) {

    user, pass, _ := req.BasicAuth()

    if user != "muuser" || pass != "mysecret" {

      // you have to import "errors"

      http.Error(w, errors.New("not authoized!!"), http. StatusUnauthorized)

        return

    }

    fmt.Printf("Requested : %s\n", req.URL.Path)


    u := &httpunix.Transport{

        DialTimeout:           100 * time.Millisecond,

        RequestTimeout:        1 * time.Second,

        ResponseHeaderTimeout: 1 * time.Second,

    }

    u.RegisterLocation("docker-socket", "/var/run/docker.sock")


    req.URL.Scheme = "http+unix"

    req.URL.Host = "docker-socket"


    resp, err := u.RoundTrip(req)


    if err != nil {

        http.Error(w, err.Error(), http.StatusServiceUnavailable)

        return

    }

    defer resp.Body.Close()

    copyHeader(w.Header(), resp.Header)

    w.WriteHeader(resp.StatusCode)

    io.Copy(w, resp.Body)

}


查看完整回答
反對 回復 2022-07-18
?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

在這里,您可以從我的以下小項目中復制邏輯。

https://github.com/alessiosavi/StreamingServer/blob/0f65dbfc77f667777d3047fa1a6b1a2cbd8aaf26/auth/authutils.go

首先,您需要一個服務器來存儲用戶(我使用過 Redis)。

比您需要3個功能的用戶

  • 登錄用戶

  • 注冊用戶

  • 刪除用戶

在登錄/注冊階段,您生成一個 cookie 散列用戶名/密碼并將 cookie 設置到 Redis 表中

比每次調用 API 時都要驗證。

隨意復制您需要的代碼。

如果某些事情不能很好理解,請打開一個問題。


查看完整回答
反對 回復 2022-07-18
  • 2 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號