From 6680b593e520e35303702dc5a234864b40ac3b1c Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Tue, 14 Sep 2021 08:39:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E6=A0=87=E5=87=86?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 34 +++++++++++++++++++++++-------- middleware/auth-middleware.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 middleware/auth-middleware.go diff --git a/go.mod b/go.mod index da74bc9..cc7782d 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module myschools.me/suguo/snippet go 1.17 require ( + github.com/gin-gonic/gin v1.7.4 github.com/hashicorp/consul/api v1.10.1 google.golang.org/grpc v1.40.0 gorm.io/driver/mysql v1.1.2 @@ -14,24 +15,41 @@ require ( require ( github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect github.com/fatih/color v1.9.0 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/go-sql-driver/mysql v1.6.0 // indirect - github.com/golang/protobuf v1.4.3 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/google/btree v1.0.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.1 // indirect github.com/hashicorp/go-hclog v0.12.0 // indirect github.com/hashicorp/go-immutable-radix v1.0.0 // indirect github.com/hashicorp/go-rootcerts v1.0.2 // indirect - github.com/hashicorp/golang-lru v0.5.0 // indirect + github.com/hashicorp/golang-lru v0.5.1 // indirect github.com/hashicorp/serf v0.9.5 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.2 // indirect + github.com/json-iterator/go v1.1.11 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/leodido/go-urn v1.2.0 // indirect github.com/mattn/go-colorable v0.1.6 // indirect github.com/mattn/go-isatty v0.0.12 // indirect github.com/mattn/go-sqlite3 v1.14.5 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect - github.com/mitchellh/mapstructure v1.1.2 // indirect - golang.org/x/net v0.0.0-20200822124328-c89045814202 // indirect - golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect - golang.org/x/text v0.3.2 // indirect - google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect - google.golang.org/protobuf v1.25.0 // indirect + github.com/mitchellh/mapstructure v1.4.1 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + github.com/stretchr/testify v1.7.0 // indirect + github.com/ugorji/go/codec v1.1.13 // indirect + golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect + golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect + golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect + golang.org/x/text v0.3.7 // indirect + google.golang.org/genproto v0.0.0-20210602131652-f16073e35f0c // indirect + google.golang.org/protobuf v1.26.0 // indirect + gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/middleware/auth-middleware.go b/middleware/auth-middleware.go new file mode 100644 index 0000000..8e2129b --- /dev/null +++ b/middleware/auth-middleware.go @@ -0,0 +1,38 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" +) + +const ( + TokenName = "token" +) + +//AuthUser 用户认证拦截器 +func AuthUser() gin.HandlerFunc { + return func(c *gin.Context) { + // tokenString := c.GetHeader(TokenName) + + // claims := &model.UserClaims{} + // tkn, err := jwt.ParseWithClaims(tokenString, claims, func(t *jwt.Token) (interface{}, error) { + // return []byte(config.JwtKey), nil + // }) + // if err != nil { + // if err == jwt.ErrSignatureInvalid { + // zt.RespUnauth(c, fmt.Sprintf("%s无效!", TokenName), nil) + // return + // } + // zt.RespUnauth(c, "请求失败", err) + // return + // } + + // if !tkn.Valid { + // zt.RespUnauth(c, fmt.Sprintf("%s 验证失败!", TokenName), nil) + // return + // } + + // c.Set("user", claims) + + // c.Next() + } +}