DKube v1.0
This commit is contained in:
24
middle/cors.go
Normal file
24
middle/cors.go
Normal file
@ -0,0 +1,24 @@
|
||||
package middle
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func Cors() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
|
||||
method := c.Request.Method
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Max-Age", "86400")
|
||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
|
||||
c.Header("Access-Control-Allow-Headers", "X-Token, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, X-Max")
|
||||
c.Header("Access-Control-Allow-Credentials", "false")
|
||||
|
||||
if method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusNoContent)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
45
middle/jwt.go
Normal file
45
middle/jwt.go
Normal file
@ -0,0 +1,45 @@
|
||||
package middle
|
||||
|
||||
import (
|
||||
"dkube/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func JWTAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if len(c.Request.URL.String()) >= 10 && c.Request.URL.String()[0:10] == "/api/login" {
|
||||
c.Next()
|
||||
} else {
|
||||
token := c.Request.Header.Get("Authorization")
|
||||
if token == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"msg": "请求未携带token,无权限访问",
|
||||
"data": nil,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
claims, err := utils.JWTToken.ParseToken(token)
|
||||
if err != nil {
|
||||
if err.Error() == "TokenExpired" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"msg": "授权已过期",
|
||||
"data": nil,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"msg": err.Error(),
|
||||
"data": nil,
|
||||
})
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
c.Set("claims", claims)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user