feat 废弃jwt 、 学识分

This commit is contained in:
2026-02-06 22:31:29 +08:00
parent b1dfb88f71
commit 456f1970eb
17 changed files with 236 additions and 156 deletions

View File

@@ -2,9 +2,10 @@ package http_gateway
import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
@@ -63,17 +64,18 @@ func authJwt() gin.HandlerFunc {
c.Abort()
return
}
claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
if err != nil {
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayAccessToken+token, (&utils.UserSession{}).GetUsnKey()).Int64()
if usn == 0 {
http_resp.JsonUnauthorized(c)
c.Abort()
return
}
// 这里将Header写到请求中grpc-gateway框架会读取然后传给grpc服务
c.Request.Header.Set("X-Usn", utils.Int64ToString(claims.USN))
c.Request.Header.Set("X-Usn", utils.Int64ToString(usn))
// 这里写到上下文中,打日志
c.Set("usn", claims.USN)
c.Set("usn", usn)
c.Next()
}
}

View File

@@ -73,7 +73,7 @@ func InitRouter() *gin.Engine {
auth.Use(authJwt())
// 网关
initBaseRoute(auth)
initGatewayPath(auth)
// 用户中心
initUserPath(auth)
// 奇怪的知识-服务端
@@ -82,14 +82,17 @@ func InitRouter() *gin.Engine {
return r
}
func initBaseRoute(r *gin.RouterGroup) {
g := r.Group("/gw")
func initGatewayPath(r *gin.RouterGroup) {
g := r.Group("/" + common.KeyDiscoverServiceNameGateway)
g.POST("/open/login", http_handler.Login)
g.POST("/open/refresh_token", http_handler.RefreshToken)
g.POST("/open/logout", http_handler.Logout)
}
func initUserPath(r *gin.RouterGroup) {
g := r.Group("/user")
g := r.Group("/" + common.KeyDiscoverServiceNameUser)
client, err := grpc_client.UserNewClientLB()
if err != nil {
log.Errorf("get user conn failed: %v", err)
@@ -106,7 +109,8 @@ func initUserPath(r *gin.RouterGroup) {
}
func initQgdzsPath(r *gin.RouterGroup) {
g := r.Group("/qgdzs")
g := r.Group("/" + common.KeyDiscoverServiceNameQgdzs)
client, err := grpc_client.QgdzsNewClientLB()
if err != nil {
log.Errorf("get qgdzs conn failed: %v", err)

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/socket"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
"go.uber.org/zap"
@@ -27,16 +25,11 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
g.logger.Warnf("token is invalid")
return socket.Close
}
claims, err := utils.ParseToken(token, config.Get().Auth.Secret)
if err != nil {
g.logger.Warnf("token is invalid")
return socket.Close
}
cli := client.NewClient(claims.USN, conn)
cli := client.NewClient(conn)
conn.SetParam("client", cli)
if !login.GetLoginQueue().AddToLoginQueue(&login.User{Cli: cli, Token: token}) {
g.logger.Warnf("AddToLoginQueue err, login queue full, usn: %v", claims.USN)
g.logger.Warnf("AddToLoginQueue err, login queue full")
return socket.Close
}
return socket.None