为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
交互式爱情 2021-08-10 21:13:41
我无法http.FileServer使用github.com/abbot/go-http-auth.package mainimport (    "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)}代码:fs := http.FileServer(http.Dir("static"))http.Handle("/static/", http.StripPrefix("/static/", fs))工作在main() 不进行认证,但是伴随不能使用它auth.JustCheck。我尝试使用handleFileServer功能,但没有显示任何内容。有什么诀窍?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 183 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信