增加gin拦截器
This commit is contained in:
parent
8fddf71e5c
commit
a490fea952
|
|
@ -0,0 +1,84 @@
|
||||||
|
package gin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"myschools.me/suguo/snippet/redis"
|
||||||
|
)
|
||||||
|
|
||||||
|
//从redis中认证用户
|
||||||
|
func AuthUser() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
token := c.GetHeader("Authorization")
|
||||||
|
claims := cacheGet(&token)
|
||||||
|
if claims == nil {
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Set("user", claims)
|
||||||
|
c.Next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//从redis中获取用户信息,最佳实践经验建议把此代码放service层
|
||||||
|
func cacheGet(token *string) interface{} {
|
||||||
|
var user interface{}
|
||||||
|
b, err := redis.GetBytes(token)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(*b, &user); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &user
|
||||||
|
}
|
||||||
|
|
||||||
|
// gin拦截,基于微服务的拦截
|
||||||
|
// func AuthUserBS() gin.HandlerFunc {
|
||||||
|
// return func(c *gin.Context) {
|
||||||
|
// token := c.GetHeader("Authorization")
|
||||||
|
// claims := userAuthWithGrpc(&token)
|
||||||
|
// if claims == nil {
|
||||||
|
// yy.RespUnauth(c, "token无效或过期,请重新登录", nil, nil)
|
||||||
|
// c.Abort()
|
||||||
|
// }
|
||||||
|
// c.Set("user", claims)
|
||||||
|
// c.Next()
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func userAuthWithGrpc(token *string) *yy.UserClaims {
|
||||||
|
// srv, err := consul.FindService("oauth", "v1")
|
||||||
|
// if err != nil {
|
||||||
|
// logrus.WithFields(logrus.Fields{
|
||||||
|
// "func": "userAuthWithGrpc",
|
||||||
|
// }).Errorf("consul.FindServer: %s", err.Error())
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// defer srv.Close()
|
||||||
|
// client := pb.NewCertificationClient(srv)
|
||||||
|
// resp, err := client.Auth(context.Background(), &pb.CertificationAuthRequest{
|
||||||
|
// Token: *token,
|
||||||
|
// })
|
||||||
|
// if err != nil {
|
||||||
|
// logrus.WithFields(logrus.Fields{
|
||||||
|
// "func": "userAuthWithGrpc",
|
||||||
|
// }).Errorf("client.Auth: %s", err.Error())
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// if resp.Result == "ok" {
|
||||||
|
// r := &yy.UserClaims{}
|
||||||
|
// if err := json.Unmarshal(resp.Data.Value, r); err != nil {
|
||||||
|
// logrus.WithFields(logrus.Fields{
|
||||||
|
// "func": "userAuthWithGrpc",
|
||||||
|
// }).Errorf("json.Unmarshal: %s", err.Error())
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
// return r
|
||||||
|
// }
|
||||||
|
// logrus.WithFields(logrus.Fields{
|
||||||
|
// "func": "userAuthWithGrpc",
|
||||||
|
// }).Warnln("nil")
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
28
go.mod
28
go.mod
|
|
@ -9,26 +9,25 @@ require (
|
||||||
github.com/hashicorp/consul/api v1.10.1
|
github.com/hashicorp/consul/api v1.10.1
|
||||||
github.com/influxdata/influxdb v1.9.5
|
github.com/influxdata/influxdb v1.9.5
|
||||||
go.mongodb.org/mongo-driver v1.7.4
|
go.mongodb.org/mongo-driver v1.7.4
|
||||||
google.golang.org/grpc v1.40.0
|
google.golang.org/grpc v1.41.0
|
||||||
gorm.io/driver/mysql v1.1.2
|
gorm.io/driver/mysql v1.1.2
|
||||||
gorm.io/driver/sqlite v1.1.4
|
gorm.io/driver/sqlite v1.1.4
|
||||||
gorm.io/gorm v1.21.15
|
gorm.io/gorm v1.21.15
|
||||||
gorm.io/plugin/dbresolver v1.1.0
|
gorm.io/plugin/dbresolver v1.1.0
|
||||||
myschools.me/wodeschool/ws-base v1.0.7
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/armon/go-metrics v0.3.3 // indirect
|
github.com/armon/go-metrics v0.3.3 // indirect
|
||||||
github.com/fatih/color v1.9.0 // indirect
|
github.com/fatih/color v1.9.0 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.13.0 // indirect
|
github.com/go-playground/locales v0.14.0 // indirect
|
||||||
github.com/go-playground/universal-translator v0.17.0 // indirect
|
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||||
github.com/go-playground/validator/v10 v10.4.1 // indirect
|
github.com/go-playground/validator/v10 v10.9.0 // indirect
|
||||||
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
github.com/go-sql-driver/mysql v1.6.0 // indirect
|
||||||
github.com/go-stack/stack v1.8.0 // indirect
|
github.com/go-stack/stack v1.8.0 // indirect
|
||||||
github.com/golang/protobuf v1.5.2 // indirect
|
github.com/golang/protobuf v1.5.2 // indirect
|
||||||
github.com/golang/snappy v0.0.1 // indirect
|
github.com/golang/snappy v0.0.1 // indirect
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 // indirect
|
github.com/google/go-cmp v0.5.6 // indirect
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
||||||
github.com/hashicorp/go-hclog v0.12.2 // indirect
|
github.com/hashicorp/go-hclog v0.12.2 // indirect
|
||||||
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
|
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
|
||||||
|
|
@ -37,29 +36,28 @@ require (
|
||||||
github.com/hashicorp/serf v0.9.5 // indirect
|
github.com/hashicorp/serf v0.9.5 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.2 // indirect
|
github.com/jinzhu/now v1.1.2 // indirect
|
||||||
github.com/json-iterator/go v1.1.11 // indirect
|
github.com/json-iterator/go v1.1.12 // indirect
|
||||||
github.com/klauspost/compress v1.13.6 // indirect
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
github.com/leodido/go-urn v1.2.0 // indirect
|
github.com/leodido/go-urn v1.2.1 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.8 // indirect
|
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.5 // indirect
|
github.com/mattn/go-sqlite3 v1.14.5 // indirect
|
||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.4.1 // indirect
|
github.com/mitchellh/mapstructure v1.4.2 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/ugorji/go/codec v1.1.13 // indirect
|
github.com/ugorji/go/codec v1.2.6 // indirect
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||||
github.com/xdg-go/scram v1.0.2 // indirect
|
github.com/xdg-go/scram v1.0.2 // indirect
|
||||||
github.com/xdg-go/stringprep v1.0.2 // indirect
|
github.com/xdg-go/stringprep v1.0.2 // indirect
|
||||||
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
|
||||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
|
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
|
||||||
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
|
golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
|
||||||
golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf // indirect
|
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
|
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
package server
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
health "myschools.me/wodeschool/ws-base/health"
|
|
||||||
)
|
|
||||||
|
|
||||||
//Server consul心跳处理
|
|
||||||
type Health struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
//Check 实现微服务接口
|
|
||||||
func (h *Health) Check(ctx context.Context, req *health.HealthCheckRequest) (*health.HealthCheckResponse, error) {
|
|
||||||
resp := &health.HealthCheckResponse{
|
|
||||||
Status: health.HealthCheckResponse_SERVING,
|
|
||||||
}
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//Watch 实现微服务接口stream
|
|
||||||
func (h *Health) Watch(req *health.HealthCheckRequest, out health.Health_WatchServer) error {
|
|
||||||
out.Send(&health.HealthCheckResponse{
|
|
||||||
Status: health.HealthCheckResponse_SERVING,
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue