컨텐츠로 건너뛰기

BasicAuth 미들웨어 사용하기

Gin에는 HTTP Basic 인증을 구현하는 내장 gin.BasicAuth() 미들웨어가 포함되어 있습니다. 사용자 이름/비밀번호 쌍의 gin.Accounts 맵(map[string]string의 단축)을 받아 적용되는 모든 라우트 그룹을 보호합니다.

package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
// 개인 데이터 시뮬레이션
var secrets = gin.H{
"foo": gin.H{"email": "[email protected]", "phone": "123433"},
"austin": gin.H{"email": "[email protected]", "phone": "666"},
"lena": gin.H{"email": "[email protected]", "phone": "523443"},
}
func main() {
router := gin.Default()
// gin.BasicAuth() 미들웨어를 사용하여 그룹화
// gin.Accounts는 map[string]string의 단축입니다
authorized := router.Group("/admin", gin.BasicAuth(gin.Accounts{
"foo": "bar",
"austin": "1234",
"lena": "hello2",
"manu": "4321",
}))
// /admin/secrets 엔드포인트
// "localhost:8080/admin/secrets"를 방문하세요
authorized.GET("/secrets", func(c *gin.Context) {
// 사용자 가져오기, BasicAuth 미들웨어에 의해 설정됨
user := c.MustGet(gin.AuthUserKey).(string)
if secret, ok := secrets[user]; ok {
c.JSON(http.StatusOK, gin.H{"user": user, "secret": secret})
} else {
c.JSON(http.StatusOK, gin.H{"user": user, "secret": "NO SECRET :("})
}
})
// 0.0.0.0:8080에서 수신 대기 및 서비스
router.Run(":8080")
}

테스트해 보기

curl의 -u 플래그를 사용하여 Basic 인증 자격 증명을 제공합니다:

Terminal window
# 인증 성공
curl -u foo:bar http://localhost:8080/admin/secrets
# => {"secret":{"email":"[email protected]","phone":"123433"},"user":"foo"}
# 잘못된 비밀번호 -- 401 Unauthorized 반환
curl -u foo:wrongpassword http://localhost:8080/admin/secrets
# 자격 증명 없음 -- 401 Unauthorized 반환
curl http://localhost:8080/admin/secrets