fix:上下文
This commit is contained in:
@@ -2,7 +2,9 @@ package basic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"fusenapi/shared"
|
||||
"fusenapi/utils/auth"
|
||||
"net/http"
|
||||
"reflect"
|
||||
@@ -10,10 +12,9 @@ import (
|
||||
"github.com/golang-jwt/jwt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DefaultJwtSecret uint64 = 21321321321
|
||||
|
||||
type IJWTParse interface {
|
||||
ParseJwtToken(r *http.Request) (jwt.MapClaims, error)
|
||||
}
|
||||
@@ -52,6 +53,7 @@ func NormalAfterLogic(w http.ResponseWriter, r *http.Request, resp *Response) {
|
||||
}
|
||||
|
||||
func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicRequest any) (*auth.UserInfo, error) {
|
||||
<<<<<<< HEAD
|
||||
// log.Println(io.ReadAll(r.Body))
|
||||
// token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息
|
||||
|
||||
@@ -112,7 +114,73 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque
|
||||
// // 白板用户
|
||||
// userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
||||
// }
|
||||
=======
|
||||
var userinfo *auth.UserInfo
|
||||
>>>>>>> 7a02b3242e440880fd2afaada55a10d50823ee92
|
||||
var err error
|
||||
// log.Println(io.ReadAll(r.Body))
|
||||
token, info, err := auth.ParseJwtTokenHeader[auth.UserInfo](r) //解析Token头, 和payload信息
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if token == "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjQyOTM0NjIsImd1ZXN0X2lkIjozNiwiaWF0IjozMTUzNjAwMCwidXNlcl9pZCI6MH0.T7PuRSrinlJu7ZZ1DVqUJLWXhY_6R1lXiUHaNdw35hU" {
|
||||
userinfo = info
|
||||
} else {
|
||||
var secret uint64 = 0
|
||||
if info != nil {
|
||||
|
||||
if info.IsUser() {
|
||||
// us, err := state.GetUserState(info.UserId) //获取缓存的用户状态
|
||||
reflect.ValueOf(svcCtx)
|
||||
ctxValue := reflect.ValueOf(svcCtx).FieldByName("MysqlConn")
|
||||
gdb := ctxValue.Interface().(*gorm.DB)
|
||||
|
||||
us, err := shared.GetUserState(info.UserId, gdb)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
secret = us.PwdHash // 获取密码的hash做jwt, 便于重置密码的使用
|
||||
|
||||
} else if info.IsGuest() {
|
||||
secret = auth.DefaultJwtSecret //获取默认的hash
|
||||
}
|
||||
}
|
||||
|
||||
if secret != 0 {
|
||||
claims, err := auth.ParseJwtTokenUint64Secret(token, secret)
|
||||
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
log.Println(token)
|
||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
||||
Code: 401, // 返回401状态码,表示未授权
|
||||
Message: "unauthorized", // 返回未授权信息
|
||||
})
|
||||
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if claims != nil {
|
||||
// 从token中获取对应的用户信息
|
||||
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||||
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||||
if err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
||||
Code: 401,
|
||||
Message: "unauthorized",
|
||||
})
|
||||
logx.Info("unauthorized:", err.Error())
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 白板用户
|
||||
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||
if err = httpx.Parse(r, LogicRequest); err != nil {
|
||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
||||
@@ -122,7 +190,7 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque
|
||||
logx.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
userinfo := &auth.UserInfo{UserId: 39}
|
||||
// userinfo := &auth.UserInfo{UserId: 39}
|
||||
return userinfo, err
|
||||
}
|
||||
|
||||
|
||||
12
utils/basic/request_parse_test.go
Normal file
12
utils/basic/request_parse_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package basic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRequestParse(t *testing.T) {
|
||||
a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("saTGjruwq7SG4vnQVEo3vsZvbfhzx8zZ3zWA+8nWVdid5tssnYQNECiP+pYCK6YhZ+LRH8m7f7JXrgyqtpYQMOhVOcNWTYAClk0Jnft6+QIPegzY9+v4k7eVMiWf5c/x")
|
||||
log.Println(a, us, err)
|
||||
}
|
||||
Reference in New Issue
Block a user