จริงๆแล้วมีวิธีการตรวจสอบและเทคนิคมากมายที่คุณสามารถเชื่อมต่อกับแอปพลิเคชันของคุณและขึ้นอยู่กับตรรกะทางธุรกิจและข้อกำหนดของแอปพลิเคชัน 
ตัวอย่างเช่น Oauth2, LDAP, การตรวจสอบท้องถิ่น ฯลฯ
คำตอบของฉันถือว่าคุณกำลังมองหาการรับรองความถูกต้องในท้องถิ่นซึ่งหมายความว่าคุณจัดการข้อมูลประจำตัวของผู้ใช้ในแอปพลิเคชันของคุณ เซิร์ฟเวอร์ต้องเปิดเผยชุดของ API ภายนอกที่อนุญาตให้ผู้ใช้และผู้ดูแลระบบจัดการบัญชีและวิธีที่พวกเขาต้องการระบุตัวเองไปยังเซิร์ฟเวอร์เพื่อให้เกิดการสื่อสารที่น่าเชื่อถือ คุณจะต้องสร้างตารางฐานข้อมูลของผู้ใช้ รหัสผ่านถูกแฮชเพื่อความปลอดภัยดูวิธีการเก็บรหัสผ่านในฐานข้อมูล
ให้ถือว่าข้อกำหนดของแอปเพื่อตรวจสอบสิทธิ์ผู้ใช้โดยใช้วิธีใดวิธีหนึ่งต่อไปนี้:
- การรับรองความถูกต้องขั้นพื้นฐาน (ชื่อผู้ใช้รหัสผ่าน): 
 วิธีการตรวจสอบความถูกต้องนี้ขึ้นอยู่กับข้อมูลประจำตัวผู้ใช้ที่กำหนดไว้ในส่วนหัวการอนุญาตที่เข้ารหัสใน base64 และกำหนดไว้ในrfc7617โดยทั่วไปเมื่อแอปได้รับคำขอของผู้ใช้ แฮถ้าตรงกับผู้ใช้รับรองความถูกต้องมิฉะนั้นส่งคืนรหัสสถานะ 401 ให้ผู้ใช้
 
- การรับรองความถูกต้องโดยใช้ใบรับรอง: 
 วิธีการรับรองความถูกต้องนี้ขึ้นอยู่กับ Digital Certificate เพื่อระบุผู้ใช้และเป็นที่รู้จักกันในชื่อ x509 auth ดังนั้นเมื่อแอปได้รับคำขอของผู้ใช้จะอ่านใบรับรองของลูกค้าและตรวจสอบว่าตรงกับใบรับรอง CA หลัก ไปที่แอป
 
- ผู้ใช้โทเค็น: 
 วิธีการรับรองความถูกต้องนี้ขึ้นอยู่กับโทเค็นการเข้าถึงระยะสั้นโทเค็นผู้ถือเป็นสตริงที่เข้ารหัสลับซึ่งมักจะสร้างโดยเซิร์ฟเวอร์เพื่อตอบสนองต่อคำขอเข้าสู่ระบบ ดังนั้นเมื่อแอปได้รับคำขอของผู้ใช้แอปจะอ่านการอนุญาตและตรวจสอบโทเค็นเพื่อตรวจสอบสิทธิ์ผู้ใช้
 
อย่างไรก็ตามฉันขอแนะนำgo-guardian  
สำหรับไลบรารี่การพิสูจน์ตัวตนซึ่งทำผ่านวิธีการพิสูจน์ตัวตนแบบขยายที่รู้จักกันในชื่อกลยุทธ์ โดยทั่วไปแล้ว Go-Guardian ไม่ได้เมานต์เส้นทางหรือสันนิษฐานว่าสคีมาฐานข้อมูลใด ๆ ซึ่งเพิ่มความยืดหยุ่นและช่วยให้ผู้พัฒนาสามารถตัดสินใจได้
การตั้งค่าตัวพิสูจน์ตัวตนของผู้พิทักษ์เป็นเรื่องตรงไปตรง 
นี่คือตัวอย่างเต็มรูปแบบของวิธีการดังกล่าว
package main
import (
    "context"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "sync"
    "github.com/golang/groupcache/lru"
    "github.com/gorilla/mux"
    "github.com/shaj13/go-guardian/auth"
    "github.com/shaj13/go-guardian/auth/strategies/basic"
    "github.com/shaj13/go-guardian/auth/strategies/bearer"
    gx509 "github.com/shaj13/go-guardian/auth/strategies/x509"
    "github.com/shaj13/go-guardian/store"
)
var authenticator auth.Authenticator
var cache store.Cache
func middleware(next http.Handler) http.HandlerFunc {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        log.Println("Executing Auth Middleware")
        user, err := authenticator.Authenticate(r)
        if err != nil {
            code := http.StatusUnauthorized
            http.Error(w, http.StatusText(code), code)
            return
        }
        log.Printf("User %s Authenticated\n", user.UserName())
        next.ServeHTTP(w, r)
    })
}
func Resource(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Resource!!\n"))
}
func Login(w http.ResponseWriter, r *http.Request) {
    token := "90d64460d14870c08c81352a05dedd3465940a7"
    user := auth.NewDefaultUser("admin", "1", nil, nil)
    cache.Store(token, user, r)
    body := fmt.Sprintf("token: %s \n", token)
    w.Write([]byte(body))
}
func main() {
    opts := x509.VerifyOptions{}
    opts.KeyUsages = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
    opts.Roots = x509.NewCertPool()
    // Read Root Ca Certificate
    opts.Roots.AddCert(readCertificate("<root-ca>"))
    cache = &store.LRU{
        lru.New(100),
        &sync.Mutex{},
    }
    // create strategies
    x509Strategy := gx509.New(opts)
    basicStrategy := basic.New(validateUser, cache)
    tokenStrategy := bearer.New(bearer.NoOpAuthenticate, cache)
    authenticator = auth.New()
    authenticator.EnableStrategy(gx509.StrategyKey, x509Strategy)
    authenticator.EnableStrategy(basic.StrategyKey, basicStrategy)
    authenticator.EnableStrategy(bearer.CachedStrategyKey, tokenStrategy)
    r := mux.NewRouter()
    r.HandleFunc("/resource", middleware(http.HandlerFunc(Resource)))
    r.HandleFunc("/login", middleware(http.HandlerFunc(Login)))
    log.Fatal(http.ListenAndServeTLS(":8080", "<server-cert>", "<server-key>", r))
}
func validateUser(ctx context.Context, r *http.Request, userName, password string) (auth.Info, error) {
    // here connect to db or any other service to fetch user and validate it.
    if userName == "stackoverflow" && password == "stackoverflow" {
        return auth.NewDefaultUser("stackoverflow", "10", nil, nil), nil
    }
    return nil, fmt.Errorf("Invalid credentials")
}
func readCertificate(file string) *x509.Certificate {
    data, err := ioutil.ReadFile(file)
    if err != nil {
        log.Fatalf("error reading %s: %v", file, err)
    }
    p, _ := pem.Decode(data)
    cert, err := x509.ParseCertificate(p.Bytes)
    if err != nil {
        log.Fatalf("error parseing certificate %s: %v", file, err)
    }
    return cert
}
การใช้งาน: 
curl  -k https://127.0.0.1:8080/login -u stackoverflow:stackoverflow
token: 90d64460d14870c08c81352a05dedd3465940a7
- รับรองความถูกต้องด้วยโทเค็น: 
curl  -k https://127.0.0.1:8080/resource -H "Authorization: Bearer 90d64460d14870c08c81352a05dedd3465940a7"
Resource!!
- รับรองความถูกต้องด้วยข้อมูลรับรองผู้ใช้:
curl  -k https://127.0.0.1:8080/resource -u stackoverflow:stackoverflow
Resource!!
- รับรองความถูกต้องด้วยใบรับรองผู้ใช้:
curl --cert client.pem --key client-key.pem --cacert ca.pem https://127.0.0.1:8080/resource
Resource!!
คุณสามารถเปิดใช้งานวิธีการรับรองความถูกต้องหลายวิธีในครั้งเดียว คุณควรใช้วิธีการอย่างน้อยสองวิธี