7 Star 24 Fork 30

go-course / go8

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
basci.go 988 Bytes
一键复制 编辑 原始数据 按行查看 历史
Mr.Yu 提交于 2022-08-14 11:25 . 基于ioc重构IMPL
package auth
import (
"net/http"
"gitee.com/go-course/go8/projects/vblog/api/conf"
"github.com/gin-gonic/gin"
"github.com/infraboard/mcube/logger/zap"
)
func BasicAuth(c *gin.Context) {
// 处理请求: 检查BasicAuth: Basic YWRtaW46MTIzNDU2
// username:password
zap.L().Debugf("basic auth: %s", c.Request.Header.Get("Authorization"))
// 1. 获取用户的用户密码
username, password, ok := c.Request.BasicAuth()
if !ok {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"code": http.StatusUnauthorized,
"message": "auth required",
})
return
}
// 和系统配置的用户密码进行比对
zap.L().Debugf("auth user: %s", username)
ac := conf.C().Auth
if !(username == ac.Username && password == ac.Password) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
"code": http.StatusUnauthorized,
"message": "username or password not conrect",
})
return
}
// 处理响应: 无
// 继续路由到后面的Handler
c.Next()
}
Go
1
https://gitee.com/go-course/go8.git
git@gitee.com:go-course/go8.git
go-course
go8
go8
6a8978f41a84

搜索帮助