英文:
How to serve static files with basic authentication?
问题
我无法使用github.com/abbot/go-http-auth
和http.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 (
"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>static file server</h1><p><a href='./static'>folder</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)
// how to secure the FileServer with basic authentication??
// 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(`Listening... http://localhost:3000
folder is ./static
authentication in map users`)
http.ListenAndServe(":3001", nil)
}
The code:
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", 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("/static/", auth.JustCheck(authenticator, handleFileServer("/tmp", "/static/")))
//....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论