forked from Mereithhh/van-nav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
80 lines (73 loc) · 1.83 KB
/
jwt.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
)
// JTW 密钥
var jwtSecret = []byte("boy_next_door")
// 签名一个 JTW
func SignJWT(user User) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": user.Name,
"id": user.Id,
"exp": time.Now().Add(time.Hour * 24).Unix(),
})
tokenString, err := token.SignedString([]byte(jwtSecret))
return tokenString, err
}
// 签名一个 JTW
func SignJWTForAPI(tokenName string, tokenId int) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": tokenName,
"id": tokenId,
"exp": time.Now().Add(time.Hour * 24 * 365 * 100).Unix(),
})
tokenString, err := token.SignedString([]byte(jwtSecret))
return tokenString, err
}
// 解密一个 JTW
func ParseJWT(tokenString string) (*jwt.Token, error) {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (i interface{}, e error) {
return jwtSecret, nil
})
return token, err
}
// 定义一个 JWT 的中间件
func JWTMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
rawToken := c.Request.Header.Get("Authorization")
if rawToken == "" {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"errorMessage": "未登录",
})
c.Abort()
return
}
// 解析 token
token, err := ParseJWT(rawToken)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"errorMessage": "未登录",
})
c.Abort()
return
}
// 把名称加到上下文
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
c.Set("username", claims["name"])
c.Set("uid", claims["id"])
c.Next()
} else {
c.JSON(http.StatusUnauthorized, gin.H{
"success": false,
"errorMessage": "未登录",
})
c.Abort()
return
}
}
}