-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_test.go
68 lines (61 loc) · 1.88 KB
/
example_test.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
package csrf_test
import (
"fmt"
"net/http"
"time"
"github.com/gogf/csrf/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/glog"
)
func ExampleNew() {
s := g.Server()
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.Middleware(csrf.New())
group.ALL("/csrf", func(r *ghttp.Request) {
reqInfo := "Request Info:\n"
reqInfo += fmt.Sprintf("%s\n", r.Method+": "+r.RequestURI)
reqInfo += fmt.Sprintf("Cookies: %v\n", r.Cookies())
reqInfo += fmt.Sprintf("Header: %v\n", r.Header)
reqInfo += fmt.Sprintf("Query: %v\n", r.URL.Query())
glog.Debug(r.Context(), reqInfo)
r.Response.Writeln(reqInfo)
})
})
s.SetAddr("127.0.0.1:8199")
s.Run()
// Get http://127.0.0.1:8199/api.v2/csrf
// get CSRF token in Cookie _csrf
// Post http://127.0.0.1:8199/api.v2/csrf
// invalid CSRF token in Header X-CSRF-Token
}
func ExampleNewWithCfg() {
s := g.Server()
s.Group("/api.v2", func(group *ghttp.RouterGroup) {
group.Middleware(csrf.NewWithCfg(csrf.Config{
Cookie: &http.Cookie{
Name: "_csrf", // token name in cookie
Secure: true,
SameSite: http.SameSiteNoneMode, // 自定义samesite
},
ExpireTime: time.Hour * 24,
TokenLength: 32,
TokenRequestKey: "X-My-Token", // use this key to read token in request param
}))
group.ALL("/csrf", func(r *ghttp.Request) {
reqInfo := "Request Info:\n"
reqInfo += fmt.Sprintf("%s\n", r.Method+": "+r.RequestURI)
reqInfo += fmt.Sprintf("Cookies: %v\n", r.Cookies())
reqInfo += fmt.Sprintf("Header: %v\n", r.Header)
reqInfo += fmt.Sprintf("Query: %v\n", r.URL.Query())
glog.Debug(r.Context(), reqInfo)
r.Response.Writeln(reqInfo)
})
})
s.SetAddr("127.0.0.1:8199")
s.Run()
// Get http://127.0.0.1:8199/api.v2/csrf
// get CSRF token in Cookie _csrf
// Post http://127.0.0.1:8199/api.v2/csrf
// invalid CSRF token in Header X-My-Token
}