-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTODO_userIsAdmin
97 lines (72 loc) · 2.86 KB
/
TODO_userIsAdmin
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
You should have the user id in the token.
The auth middleware (Authenticate function) should extract that user ID,
and put it in the request context, so the actual handlers can use it.
There are other ways to do it, but this is common and straightforward. –
Burak Serdar
Aug 14 at 17:03
do you mean, I should set to the context like context.Set(r, UserId, getClaim.userId) and get in handlers .. can I set a struct (user := &models.User{}) to it? –
itx
Aug 14 at 17:33
You can put the user struct in request context,
and use that context downstream: ctx:=r.Context(), ctx.Set("user",userStruct); r=r.WithContext(ctx). Then handlers can get the user struct from request context. –
Burak Serdar
Aug 14 at 1
# 这里也可以确实是不是管理员
// Set claims
// This is the information which frontend can use
// The backend can also decode the token and get admin etc.
claims := token.Claims.(jwt.MapClaims)
claims["name"] = "Jon Doe"
claims["admin"] = true
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
unc (h *handler) private(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
name := claims["name"].(string)
return c.String(http.StatusOK, "Welcome "+name+"!")
func isAdmin(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
isAdmin := claims["admin"].(bool)
if isAdmin == false {
return echo.ErrUnauthorized
}
return next(c)
}
}
c.Keys["username"]
Customer Claims
又可將其稱作Payload,以Json的形式將使用者相關訊息,甚至是過期時間、簽證發放時間都寫在這
app/middleware/jwt-token.go
// MyClaims Customer jwt.StandardClaims
type MyClaims struct {
Account string `json:"account"`
jwt.StandardClaims
}
這邊我們自定義一個MyClaims,並除了jwt-go原本的jwt.StandardClaims外,我們還另外儲存了Account的資訊
请使用python 编写一个爬虫,在sg.iherb.com网站创建一个账号,用户名为[email protected], 密码为111000@sina,并添加一个商品到购物车,请给出python实现代码
methods: {
handleRegister(user) {
this.message = "";
this.successful = false;
this.loading = true;
this.$store.dispatch("auth/register", user).then(
(data) => {
this.message = data.message;
this.successful = true;
this.loading = false;
},
(error) => {
this.message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
this.successful = false;
this.loading = false;
}
);
},
},