WeChatFerry/clients/gohttp/httpd/midware/guard.go
2023-12-18 08:48:14 +08:00

28 lines
437 B
Go

package midware
import (
"strings"
"github.com/gin-gonic/gin"
"wechat-rest/args"
)
func AuthGuard(c *gin.Context) {
token := ""
authcode := c.GetHeader("Authorization")
parts := strings.SplitN(authcode, " ", 2)
if len(parts) == 2 && parts[0] == "Bearer" {
token = parts[1]
}
if token != args.Httpd.Token {
c.Set("Error", gin.H{"Code": 401, "Message": "未授权的操作"})
c.Set("ExitCode", 401)
c.Abort()
}
}