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
}
posted @ 2021-12-01 09:56  學海無涯  阅读(145)  评论(0编辑  收藏  举报