【问题标题】:Basic Authentication for static resources静态资源的基本认证
【发布时间】:2018-09-28 04:41:30
【问题描述】:

如何向我的静态资源添加基本身份验证?使用下面的代码,我可以查看标签文件夹中的任何文件。我知道在this 问题中解释了如何做到这一点。但是如果不使用http.ResponseWriter,我将如何设置标题?

package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
    "os"
)

func main() {
    port := GetPort()
    log.Println("[-] Listening on...", port)

    r := mux.NewRouter()
    r.PathPrefix("/labels/").Handler(http.StripPrefix("/labels/", http.FileServer(http.Dir("./labels/"))))

    err := http.ListenAndServe(port, r)
    log.Fatal(err)
}

// GetPort is for herkou deployment
func GetPort() string {
    port := os.Getenv("PORT")
    if port == "" {
        port = "4747"
        log.Println("[-] No PORT environment variable detected. Setting to ", port)
    }
    return ":" + port
}

【问题讨论】:

  • 用身份验证中间件包装您的处理程序,以允许您的每个请求都通过身份验证中间件。
  • 为什么不从处理程序中提供 pdf 文件,用户可以在其中点击端点获取 pdf 文件,然后您可以在验证后使用 http 下载文件。您当前的方法不好,因为您正在提供所有静态文件并希望对其进行验证。所以最好只对可以使用页面上的链接下载的 pdf 文件进行验证。
  • @Himanshu 这听起来是个好主意。抱歉,我是新手,所以我不知道该怎么做。
  • 按照我在回答中提供的流程就可以了。正常提供 html、css、js 等静态文件,然后提供一个 URL,您应该像我一样在该 URL 上创建一个处理程序,然后在下载文件之前为用户添加身份验证以通过。让我编辑我的答案。

标签: http authentication go basic-authentication gorilla


【解决方案1】:

围绕每个处理程序创建一个包装器,以传递来自身份验证中间件的请求,该请求将在身份验证完成后进一步转发请求,否则返回错误响应

func authentication(next http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    log.Println("Executing authentication")
    next.ServeHTTP(w, r)
  })
}

// open the dialog to download pdf files.
func dowloadPdf(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Disposition", "attachment; filename=YOUR_FILE")
    w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
    w.Write([]byte("File downloaded"))
}

func main(){
     pdfHandler := http.HandlerFunc(dowloadPdf)
     http.Handle("/servepdf", authentication(pdfHandler))
     http.ListenAndServe(":3000", nil)
}

但如果我考虑到在提供静态文件(如 html、css、js 等)时不需要进行身份验证。最好在验证用户身份后创建一个处理程序来提供 pdf 文件。

您还可以将 negorni 中间件与 gorilla mux 一起使用,而不是创建自定义中间件。

【讨论】:

  • 我看不出这在我现有的代码中是如何工作的。另外,我有需要对其应用基本身份验证的 pdf 文件。
  • @TylerZika 只需将中间件与您的 gorilla mux 路由器一起使用。
  • @TylerZika 我已经更新了我的答案。您还可以做的另一件事是使用带有 gorilla mux 的 negroni 中间件,而不是像我一样使用自定义。
【解决方案2】:
package main

import (
    "github.com/gorilla/mux"
    "log"
    "net/http"
    "os"
)

func main() {
    port := GetPort()
    log.Println("[-] Listening on...", port)

    r := mux.NewRouter()
    r.PathPrefix("/labels/").Handler(http.StripPrefix("/labels/", ServeLabels(http.FileServer(http.Dir("./labels/")))))

    err := http.ListenAndServe(port, r)
    log.Fatal(err)
}

func ServeLabels(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("WWW-Authenticate", `Basic realm="mydomain"`)
        h.ServeHTTP(w, r)
    })
}

// GetPort is for herkou deployment
func GetPort() string {
    port := os.Getenv("PORT")
    if port == "" {
        port = "4747"
        log.Println("[-] No PORT environment variable detected. Setting to ", port)
    }
    return ":" + port
}

类似这样,或者您可以继续使用 gorilla mux 中间件。

【讨论】:

  • 当您在网络浏览器中访问http://localhost:4747/labels/ 时,会出现身份验证窗口?这不会发生在我身上。
猜你喜欢
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 2013-08-01
  • 2014-08-29
  • 1970-01-01
  • 2014-07-29
相关资源
最近更新 更多