package middlewares import ( "git.sxidc.com/go-framework/baize/convenient/domain/auth/jwt_tools" "git.sxidc.com/go-framework/baize/convenient/domain/auth/user" "git.sxidc.com/go-framework/baize/framework/binding" "git.sxidc.com/go-framework/baize/framework/core/api" "git.sxidc.com/go-framework/baize/framework/core/api/response" "git.sxidc.com/go-framework/baize/framework/core/domain" "git.sxidc.com/go-framework/baize/framework/core/infrastructure" "github.com/dgrijalva/jwt-go/request" "github.com/pkg/errors" "net/http" ) var dbSchema string func SetDBSchema(schema string) { dbSchema = schema } func Authentication() binding.Middleware { return func(c *api.Context, i *infrastructure.Infrastructure) { respFunc := response.SendMapResponse // 获取token token, err := request.AuthorizationHeaderExtractor.ExtractToken(c.Request) if err != nil { respFunc(c, http.StatusUnauthorized, nil, errors.New(err.Error())) c.Abort() return } // 校验token valid, _, err := jwt_tools.CheckJWT(token) if err != nil { respFunc(c, http.StatusUnauthorized, nil, errors.New(err.Error())) c.Abort() return } if !valid { respFunc(c, http.StatusUnauthorized, nil, errors.New("无效token")) c.Abort() return } // 获取用户信息 userTableName := domain.TableName(simple.Schema, &user.Entity{}) dbExecutor := i.DBExecutor() c.Next() } }