qnc-server-tob/common/middleware/commonJwtAuthMiddleware.go

32 lines
727 B
Go
Raw Normal View History

2025-01-10 00:09:25 +08:00
package middleware
import (
"github.com/zeromicro/go-zero/rest/handler"
"net/http"
)
// CommonJwtAuthMiddleware : with jwt on the verification, no jwt on the verification
type CommonJwtAuthMiddleware struct {
secret string
}
func NewCommonJwtAuthMiddleware(secret string) *CommonJwtAuthMiddleware {
return &CommonJwtAuthMiddleware{
secret: secret,
}
}
func (m *CommonJwtAuthMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if len(r.Header.Get("Authorization")) > 0 {
//has jwt Authorization
authHandler := handler.Authorize(m.secret)
authHandler(next).ServeHTTP(w, r)
return
} else {
//no jwt Authorization
next(w, r)
}
}
}