fix
This commit is contained in:
parent
9b6396dfae
commit
1a45340882
|
@ -2,6 +2,7 @@ package basic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
@ -53,12 +54,12 @@ 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) {
|
func ParseJwtToken(r *http.Request, svcCtx any) (*auth.UserInfo, error) {
|
||||||
var userinfo *auth.UserInfo
|
var userinfo *auth.UserInfo
|
||||||
var err error
|
var err error
|
||||||
// log.Println(io.ReadAll(r.Body))
|
// log.Println(io.ReadAll(r.Body))
|
||||||
token := r.Header.Get("Authorization")
|
token := r.Header.Get("Authorization")
|
||||||
userId, err := strconv.ParseInt(token, 10, 63)
|
userId, err := strconv.ParseInt(token, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
userinfo = &auth.UserInfo{
|
userinfo = &auth.UserInfo{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
|
@ -97,11 +98,9 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque
|
||||||
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(token)
|
log.Println(token)
|
||||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
if err != nil {
|
||||||
Code: 401, // 返回401状态码,表示未授权
|
return nil, fmt.Errorf("unauthorized")
|
||||||
Message: "unauthorized", // 返回未授权信息
|
}
|
||||||
})
|
|
||||||
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -110,12 +109,7 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque
|
||||||
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||||||
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
return nil, fmt.Errorf("unauthorized")
|
||||||
Code: 401,
|
|
||||||
Message: "unauthorized",
|
|
||||||
})
|
|
||||||
logx.Info("unauthorized:", err.Error())
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -124,6 +118,20 @@ func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicReque
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return userinfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RequestParse(w http.ResponseWriter, r *http.Request, svcCtx any, LogicRequest any) (*auth.UserInfo, error) {
|
||||||
|
|
||||||
|
// 新的解析jwtToken
|
||||||
|
userinfo, err := ParseJwtToken(r, svcCtx)
|
||||||
|
if err != nil {
|
||||||
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
||||||
|
Code: 510,
|
||||||
|
Message: err.Error(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||||
if err = httpx.Parse(r, LogicRequest); err != nil {
|
if err = httpx.Parse(r, LogicRequest); err != nil {
|
||||||
httpx.OkJsonCtx(r.Context(), w, &Response{
|
httpx.OkJsonCtx(r.Context(), w, &Response{
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package basic
|
package basic
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/base64"
|
||||||
"fusenapi/utils/auth"
|
"fusenapi/utils/auth"
|
||||||
"log"
|
"log"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -10,3 +12,16 @@ func TestRequestParse(t *testing.T) {
|
||||||
a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("saTGjruwq7SG4vnQVEo3vsZvbfhzx8zZ3zWA+8nWVdid5tssnYQNECiP+pYCK6YhZ+LRH8m7f7JXrgyqtpYQMOhVOcNWTYAClk0Jnft6+QIPegzY9+v4k7eVMiWf5c/x")
|
a, us, err := auth.TParseJwtTokenHeader[auth.UserInfo]("saTGjruwq7SG4vnQVEo3vsZvbfhzx8zZ3zWA+8nWVdid5tssnYQNECiP+pYCK6YhZ+LRH8m7f7JXrgyqtpYQMOhVOcNWTYAClk0Jnft6+QIPegzY9+v4k7eVMiWf5c/x")
|
||||||
log.Println(a, us, err)
|
log.Println(a, us, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHash(t *testing.T) {
|
||||||
|
a := "fs12345678"
|
||||||
|
h := sha256.New()
|
||||||
|
h.Write([]byte(a))
|
||||||
|
s := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
|
||||||
|
key := auth.StringToHash(s)
|
||||||
|
// log.Println(auth.GenerateJwtTokenUint64(auth.StringToHash(s), 400000000, time.Now().UTC().Unix(), 39, 0))
|
||||||
|
authkey := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIwOTMxOTIyMjYsImd1ZXN0X2lkIjowLCJpYXQiOjE2OTMxOTIyMjYsInVzZXJfaWQiOjM5fQ.DrvtD7gKB0gz1rAOAQHSnyBK3exTFqoLlacpZiadpB4"
|
||||||
|
log.Println(authkey)
|
||||||
|
log.Println(auth.TParseJwtTokenHeader[auth.UserInfo](authkey))
|
||||||
|
log.Println(auth.ParseJwtTokenUint64Secret(authkey, key))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user