修改jwt拦截处理,添加错误问题
This commit is contained in:
parent
0a02b2e6dd
commit
b1205306e4
24
main.go
24
main.go
|
|
@ -100,18 +100,6 @@ func writeLogger(c *gin.Context) {
|
||||||
}
|
}
|
||||||
go dao.InsertLogToDB(path, ip, method, params)
|
go dao.InsertLogToDB(path, ip, method, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
type CustomClaims struct {
|
|
||||||
ID int `json:"id"`
|
|
||||||
Username string `json:"username"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c CustomClaims) Valid() error {
|
|
||||||
//TODO implement me
|
|
||||||
panic("custom claims valid not implement yet!")
|
|
||||||
}
|
|
||||||
|
|
||||||
func JWTAuthMiddleware() gin.HandlerFunc {
|
func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if proto.Config.LOG_SAVE_DAYS > 0 {
|
if proto.Config.LOG_SAVE_DAYS > 0 {
|
||||||
|
|
@ -168,9 +156,7 @@ func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
//token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
//token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||||
// return proto.SigningKey, nil
|
// return proto.SigningKey, nil
|
||||||
//})
|
//})
|
||||||
|
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
|
||||||
claims := &CustomClaims{}
|
|
||||||
token, err := jwt.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (interface{}, error) {
|
|
||||||
// 验证签名算法
|
// 验证签名算法
|
||||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
return nil, jwt.ErrSignatureInvalid
|
return nil, jwt.ErrSignatureInvalid
|
||||||
|
|
@ -196,13 +182,11 @@ func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将用户信息添加到上下文中
|
// 将用户信息添加到上下文中
|
||||||
c.Set("id", token.Claims.(jwt.MapClaims)["id"])
|
|
||||||
c.Set("username", token.Claims.(jwt.MapClaims)["username"])
|
|
||||||
id := token.Claims.(jwt.MapClaims)["id"]
|
id := token.Claims.(jwt.MapClaims)["id"]
|
||||||
userID := int(id.(float64))
|
c.Set("id", id)
|
||||||
c.Set("user_id", userID)
|
c.Set("user_id", int(id.(float64)))
|
||||||
|
|
||||||
if UserFuncIntercept(int(token.Claims.(jwt.MapClaims)["id"].(float64)), c.Request.URL.Path) {
|
if UserFuncIntercept(int(id.(float64)), c.Request.URL.Path) {
|
||||||
c.AbortWithStatusJSON(http.StatusOK, gin.H{"message": "unauthorized", "error": "no function permission", "code": proto.NoPermission})
|
c.AbortWithStatusJSON(http.StatusOK, gin.H{"message": "unauthorized", "error": "no function permission", "code": proto.NoPermission})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue