如何使用基本身份验证来提供静态文件?

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

How to serve static files with basic authentication?

问题

我无法使用github.com/abbot/go-http-authhttp.FileServer一起实现基本身份验证。

以下是翻译的代码部分:

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/abbot/go-http-auth"
)

func Secret(user, realm string) string {
	users := map[string]string{
		"john": "$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1", //hello
	}

	if a, ok := users[user]; ok {
		return a
	}
	return ""
}

func doRoot(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "<h1>静态文件服务器</h1><p><a href='./static'>文件夹</p>")
}

func handleFileServer(w http.ResponseWriter, r *http.Request) {
	fs := http.FileServer(http.Dir("static"))
	http.StripPrefix("/static/", fs)
}

func main() {

	authenticator := auth.NewBasicAuthenticator("localhost", Secret)

	// 如何使用基本身份验证保护FileServer?
	// fs := http.FileServer(http.Dir("static"))
	// http.Handle("/static/", http.StripPrefix("/static/", fs))

	http.HandleFunc("/static/", auth.JustCheck(authenticator, handleFileServer))

	http.HandleFunc("/", auth.JustCheck(authenticator, doRoot))

	log.Println(`监听中... http://localhost:3000
	文件夹为 ./static
	用户身份验证在用户映射中`)
	http.ListenAndServe(":3001", nil)
}

main()函数中,以下代码:

fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))

可以在没有身份验证的情况下正常工作,但无法与auth.JustCheck一起使用。我尝试使用handleFileServer函数,但没有显示任何内容。有什么诀窍吗?

英文:

I cannot make basic authentication work with http.FileServer using github.com/abbot/go-http-auth.

package main

import (
	&quot;fmt&quot;
	&quot;log&quot;
	&quot;net/http&quot;

	&quot;github.com/abbot/go-http-auth&quot;
)

func Secret(user, realm string) string {
	users := map[string]string{
		&quot;john&quot;: &quot;$1$dlPL2MqE$oQmn16q49SqdmhenQuNgs1&quot;, //hello
	}

	if a, ok := users[user]; ok {
		return a
	}
	return &quot;&quot;
}

func doRoot(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, &quot;&lt;h1&gt;static file server&lt;/h1&gt;&lt;p&gt;&lt;a href=&#39;./static&#39;&gt;folder&lt;/p&gt;&quot;)
}

func handleFileServer(w http.ResponseWriter, r *http.Request) {
	fs := http.FileServer(http.Dir(&quot;static&quot;))
	http.StripPrefix(&quot;/static/&quot;, fs)
}

func main() {

	authenticator := auth.NewBasicAuthenticator(&quot;localhost&quot;, Secret)

	// how to secure the FileServer with basic authentication??
	// fs := http.FileServer(http.Dir(&quot;static&quot;))
	// http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, fs))

	http.HandleFunc(&quot;/static/&quot;, auth.JustCheck(authenticator, handleFileServer))

	http.HandleFunc(&quot;/&quot;, auth.JustCheck(authenticator, doRoot))

	log.Println(`Listening... http://localhost:3000
 folder is ./static
 authentication in map users`)
	http.ListenAndServe(&quot;:3001&quot;, nil)
}

The code:

fs := http.FileServer(http.Dir(&quot;static&quot;))
http.Handle(&quot;/static/&quot;, http.StripPrefix(&quot;/static/&quot;, fs))

works in main() without authentication, but cannot use it along with auth.JustCheck. I tried with handleFileServer function, but nothing is displayed. What's the trick?

答案1

得分: 10

你需要返回StripPrefix的ServeHTTP方法,例如:

func handleFileServer(dir, prefix string) http.HandlerFunc {
    fs := http.FileServer(http.Dir(dir))
    realHandler := http.StripPrefix(prefix, fs).ServeHTTP
    return func(w http.ResponseWriter, req *http.Request) {
        log.Println(req.URL)
        realHandler(w, req)
    }
}

func main() {
    //....
    http.HandleFunc("/static/", auth.JustCheck(authenticator, handleFileServer("/tmp", "/static/")))
    //....
}
英文:

You need to return StripPrefix's ServeHTTP method, for example:

func handleFileServer(dir, prefix string) http.HandlerFunc {
	fs := http.FileServer(http.Dir(dir))
	realHandler := http.StripPrefix(prefix, fs).ServeHTTP
	return func(w http.ResponseWriter, req *http.Request) {
		log.Println(req.URL)
		realHandler(w, req)
	}
}
 
func main()
    //....
    http.HandleFunc(&quot;/static/&quot;, auth.JustCheck(authenticator, handleFileServer(&quot;/tmp&quot;, &quot;/static/&quot;)))
    //....
}

huangapple
  • 本文由 发表于 2014年8月28日 23:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/25552107.html
匿名

发表评论

匿名网友

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

确定