first
This commit is contained in:
31
common/middleware/commonJwtAuthMiddleware.go
Normal file
31
common/middleware/commonJwtAuthMiddleware.go
Normal file
@@ -0,0 +1,31 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user