WEB 基础认证(BasicAuth)

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	r := gin.Default()

	r.GET("/", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"message": "Hello World!",
		})
	})

	authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
		"user1": "root",
		"user2": "root",
		"user3": "root",
	}))

	authorized.GET("/secret", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"secret": "The secret ingredient to the BBQ sauce is stiring it in an old whiskey barrel.",
		})
	})

	r.Run() // 监听服务在 0.0.0.0:8080
}
转载请注明出处并保持作品的完整性,谢谢
原文地址:https://www.cnblogs.com/cheungxiongwei/p/15627613.html