2.go做baseic

代码

package main
import (
    "encoding/base64"
    "log"
    "net/http"
    "os"
    "os/signal"
    "strings"
)
type webHandler struct {

}
func (webHandler) ServeHTTP (w http.ResponseWriter, request *http.Request ){
    auth:= request.Header.Get("Authorization")
    if auth == "" {
        w.Header().Set("WWW-Authenticate",`Basic realm=11111"`)
        w.WriteHeader(http.StatusUnauthorized)
        return
    }
    authList:=strings.Split(auth," ")
    if len(authList) ==2 && authList[0] =="Basic" {
        res,err:= base64.StdEncoding.DecodeString(authList[1])//base64解码
        if err == nil && string(res) == "zhang:123" {
            w.Write([]byte("<h1>进来了</h1>"))
            return
        }
    }
    w.Write([]byte("用户名密码错误"))
}
func main(){
    c:=make(chan os.Signal)//信号
    go func() {
        http.ListenAndServe(":9091",webHandler{})
    }()
    signal.Notify(c,os.Interrupt)
    s:=<-c
    log.Println(s)
}

 

posted on 2022-11-13 23:10  孤灯引路人  阅读(19)  评论(0编辑  收藏  举报

导航