-
Notifications
You must be signed in to change notification settings - Fork 4
/
middlewares.go
39 lines (36 loc) · 983 Bytes
/
middlewares.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package app
import (
"errors"
"fmt"
"log"
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/mizhexiaoxiao/k8s-api-service/config"
)
//简单校验
func Auth() gin.HandlerFunc {
return func(c *gin.Context) {
appG := Gin{C: c}
url := appG.C.Request.RequestURI
if url == "/api/v1/health_check" || strings.HasPrefix(url, "/swagger/") {
c.Next()
return
}
appKey := appG.C.GetHeader("appKey")
appSecret := appG.C.GetHeader("appSecret")
configSecret := config.GetString("caller" + "." + appKey + ".secret")
if configSecret == "" {
appG.Fail(http.StatusInternalServerError, errors.New(fmt.Sprintf("Please carry appKey and appSecret in the request header")), nil)
log.Println(errors.New(fmt.Sprintf("Please carry appKey and appSecret in the request header")))
c.Abort()
return
}
if appSecret != configSecret {
appG.Fail(http.StatusUnauthorized, errors.New("Authentication failed"), nil)
c.Abort()
return
}
c.Next()
}
}