SSE 推送替代轮训
This commit is contained in:
@@ -16,11 +16,19 @@ const (
|
||||
ContextUsername = "username"
|
||||
)
|
||||
|
||||
func extractToken(c *gin.Context) string {
|
||||
header := c.GetHeader("Authorization")
|
||||
tokenText := strings.TrimSpace(strings.TrimPrefix(header, "Bearer "))
|
||||
if tokenText != "" && tokenText != header {
|
||||
return tokenText
|
||||
}
|
||||
return c.Query("token")
|
||||
}
|
||||
|
||||
func Auth(jwtManager *auth.JWTManager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
header := c.GetHeader("Authorization")
|
||||
tokenText := strings.TrimSpace(strings.TrimPrefix(header, "Bearer "))
|
||||
if tokenText == "" || tokenText == header {
|
||||
tokenText := extractToken(c)
|
||||
if tokenText == "" {
|
||||
response.Unauthorized(c, "缺少访问令牌")
|
||||
c.Abort()
|
||||
return
|
||||
@@ -41,9 +49,8 @@ func Auth(jwtManager *auth.JWTManager) gin.HandlerFunc {
|
||||
|
||||
func AdminAuth(jwtManager *auth.JWTManager) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
header := c.GetHeader("Authorization")
|
||||
tokenText := strings.TrimSpace(strings.TrimPrefix(header, "Bearer "))
|
||||
if tokenText == "" || tokenText == header {
|
||||
tokenText := extractToken(c)
|
||||
if tokenText == "" {
|
||||
response.Unauthorized(c, "缺少后台访问令牌")
|
||||
c.Abort()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user