Skip to content

Commit 612f017

Browse files
author
zhoujun
committed
init
1 parent ca79602 commit 612f017

23 files changed

+2725
-55
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.exe
22
.idea
3-
*.test
3+
*.test
4+
#https://www.cnblogs.com/xiao-xue-di/p/14428435.html

api/user.go

-1
This file was deleted.

go.mod

+34-10
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,44 @@ require (
88
)
99

1010
require (
11+
github.com/dgrijalva/jwt-go v3.2.0+incompatible
12+
github.com/go-playground/universal-translator v0.17.0
13+
github.com/go-playground/validator/v10 v10.4.1
14+
github.com/golang-jwt/jwt v3.2.2+incompatible
15+
github.com/spf13/viper v1.10.1
16+
google.golang.org/grpc v1.43.0
17+
google.golang.org/protobuf v1.27.1
18+
)
19+
20+
require (
21+
github.com/fsnotify/fsnotify v1.5.1 // indirect
1122
github.com/gin-contrib/sse v0.1.0 // indirect
1223
github.com/go-playground/locales v0.13.0 // indirect
13-
github.com/go-playground/universal-translator v0.17.0 // indirect
14-
github.com/go-playground/validator/v10 v10.4.1 // indirect
15-
github.com/golang/protobuf v1.3.3 // indirect
16-
github.com/json-iterator/go v1.1.9 // indirect
24+
github.com/golang/protobuf v1.5.2 // indirect
25+
github.com/hashicorp/hcl v1.0.0 // indirect
26+
github.com/json-iterator/go v1.1.12 // indirect
27+
github.com/kr/pretty v0.2.0 // indirect
1728
github.com/leodido/go-urn v1.2.0 // indirect
18-
github.com/mattn/go-isatty v0.0.12 // indirect
19-
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
20-
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
29+
github.com/magiconair/properties v1.8.5 // indirect
30+
github.com/mattn/go-isatty v0.0.14 // indirect
31+
github.com/mitchellh/mapstructure v1.4.3 // indirect
32+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
33+
github.com/modern-go/reflect2 v1.0.2 // indirect
34+
github.com/pelletier/go-toml v1.9.4 // indirect
35+
github.com/spf13/afero v1.6.0 // indirect
36+
github.com/spf13/cast v1.4.1 // indirect
37+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
38+
github.com/spf13/pflag v1.0.5 // indirect
39+
github.com/subosito/gotenv v1.2.0 // indirect
2140
github.com/ugorji/go/codec v1.1.7 // indirect
2241
go.uber.org/atomic v1.7.0 // indirect
2342
go.uber.org/multierr v1.6.0 // indirect
24-
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
25-
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
26-
gopkg.in/yaml.v2 v2.2.8 // indirect
43+
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
44+
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
45+
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
46+
golang.org/x/text v0.3.7 // indirect
47+
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
48+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
49+
gopkg.in/ini.v1 v1.66.2 // indirect
50+
gopkg.in/yaml.v2 v2.4.0 // indirect
2751
)

go.sum

+720-10
Large diffs are not rendered by default.

main.go

-33
This file was deleted.

user-web/a_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
import "github.com/golang-jwt/jwt"
8+
9+
// For HMAC signing method, the key can be any []byte. It is recommended to generate
10+
// a key using crypto/rand or something equivalent. You need the same key for signing
11+
// and validating.
12+
var hmacSampleSecret = []byte{1, 2, 3}
13+
14+
func TestA(t *testing.T) {
15+
16+
// Create a new token object,
17+
// specifying signing method and the claims you would like it to contain.
18+
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
19+
"foo": "bar",
20+
"nbf": "1",
21+
})
22+
23+
// Sign and get the complete encoded token as a string using the secret
24+
tokenString, err := token.SignedString(hmacSampleSecret)
25+
26+
fmt.Println(tokenString, err)
27+
}
28+
29+
func TestB(t *testing.T) {
30+
tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.aGTWgif4pwMnjF8My859yqoueBN9ueg95F58WNFt1ps"
31+
32+
// Parse takes the token string and a function for looking up the key. The latter is especially
33+
// useful if you use multiple keys for your application. The standard is to use 'kid' in the
34+
// head of the token to identify which key to use, but the parsed token (head and claims) is provided
35+
// to the callback, providing flexibility.
36+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
37+
// Don't forget to validate the alg is what you expect:
38+
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
39+
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
40+
}
41+
42+
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
43+
return hmacSampleSecret, nil
44+
})
45+
46+
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
47+
fmt.Println(claims["foo"], claims["nbf"])
48+
} else {
49+
fmt.Println(err)
50+
}
51+
52+
}

0 commit comments

Comments
 (0)