From 456f1970eb2ab494a3c3fcb0f1dd5f1877f70f06 Mon Sep 17 00:00:00 2001 From: "DESKTOP-V763RJ7\\Administrator" <835606593@qq.com> Date: Fri, 6 Feb 2026 22:31:29 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=BA=9F=E5=BC=83jwt=20=E3=80=81=20?= =?UTF-8?q?=E5=AD=A6=E8=AF=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/app.go | 10 +- app/base.go | 27 ----- config/config.dev.yaml | 5 +- config/config.go | 5 +- config/config.prod.yaml | 5 +- go.mod | 6 +- go.sum | 4 +- internal/global/global.go | 5 +- internal/handler/http_handler/login.go | 76 +++++++------ internal/handler/http_handler/login_test.go | 88 +++++++-------- internal/handler/http_handler/session.go | 108 +++++++++++++++++++ internal/handler/ws_handler/client/client.go | 10 +- internal/handler/ws_handler/login/login.go | 5 +- internal/net/http_gateway/middleward.go | 12 ++- internal/net/http_gateway/router.go | 14 ++- internal/net/ws_gateway/server.go | 11 +- internal/testutil/suite.go | 1 - 17 files changed, 236 insertions(+), 156 deletions(-) delete mode 100644 app/base.go create mode 100644 internal/handler/http_handler/session.go diff --git a/app/app.go b/app/app.go index da93364..d76b3b3 100644 --- a/app/app.go +++ b/app/app.go @@ -17,11 +17,10 @@ type Program struct { } func (p *Program) Init(_ svc.Environment) error { - base := &ModuleBase{} - if err := base.Init(); err != nil { + if err := config.LoadConfig(); err != nil { return err } - p.moduleList = append(p.moduleList, base) + p.moduleList = append(p.moduleList, (&module.Base{}).Bind(config.Get().Log)) p.moduleList = append(p.moduleList, (&module.DB{}).Bind(config.Get().DB, config.Get().App.Name)) p.moduleList = append(p.moduleList, &ModuleWebServer{}) p.moduleList = append(p.moduleList, &ModuleWebsocketServer{}) @@ -31,10 +30,7 @@ func (p *Program) Init(_ svc.Environment) error { p.moduleList = append(p.moduleList, (&module.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway)) p.moduleList = append(p.moduleList, &module.Discover{}) - for i, m := range p.moduleList { - if i == 0 { - continue - } + for _, m := range p.moduleList { if err := m.Init(); err != nil { return err } diff --git a/app/base.go b/app/base.go deleted file mode 100644 index dcafd8e..0000000 --- a/app/base.go +++ /dev/null @@ -1,27 +0,0 @@ -package app - -import ( - "git.hlsq.asia/mmorpg/service-common/log" - "git.hlsq.asia/mmorpg/service-common/module" - "git.hlsq.asia/mmorpg/service-common/utils" - "git.hlsq.asia/mmorpg/service-gateway/config" - "math/rand" -) - -// ModuleBase 基础模块,或者一些零散的模块 -type ModuleBase struct { - module.DefaultModule -} - -func (m *ModuleBase) Init() error { - // 配置 - if err := config.LoadConfig(); err != nil { - return err - } - cfg := config.Get() - // 日志 - log.Init(cfg.Log.Debug, cfg.Log.MaxSize, cfg.Log.MaxBackups, cfg.Log.MaxAge, cfg.Log.Level) - // 雪花 - utils.InitSnowflake(int64(rand.Intn(1000))) - return nil -} diff --git a/config/config.dev.yaml b/config/config.dev.yaml index ed3bda7..ea34370 100644 --- a/config/config.dev.yaml +++ b/config/config.dev.yaml @@ -40,6 +40,5 @@ serve: port: 18503 auth: - secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU=" - shortExpire: 60 - longExpire: 10080 + shortExpire: 3600 + longExpire: 604800 diff --git a/config/config.go b/config/config.go index f18de65..fdbba6e 100644 --- a/config/config.go +++ b/config/config.go @@ -14,9 +14,8 @@ type Config struct { } type AuthConfig struct { - Secret string `yaml:"secret"` - ShortExpire int64 `yaml:"shortExpire"` - LongExpire int64 `yaml:"longExpire"` + ShortExpire int64 `yaml:"shortExpire"` + LongExpire int64 `yaml:"longExpire"` } var cfg *Config diff --git a/config/config.prod.yaml b/config/config.prod.yaml index ccc1213..693e13a 100644 --- a/config/config.prod.yaml +++ b/config/config.prod.yaml @@ -40,6 +40,5 @@ serve: port: 18503 auth: - secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU=" - shortExpire: 15 - longExpire: 10080 \ No newline at end of file + shortExpire: 3600 + longExpire: 604800 \ No newline at end of file diff --git a/go.mod b/go.mod index 1d23998..738a7e0 100644 --- a/go.mod +++ b/go.mod @@ -4,16 +4,16 @@ go 1.24.0 require ( bou.ke/monkey v1.0.2 - git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed + git.hlsq.asia/mmorpg/service-common v0.0.0-20260206142243-44ebbe444953 github.com/alicebob/miniredis/v2 v2.35.0 github.com/gin-contrib/cors v1.7.6 github.com/gin-gonic/gin v1.11.0 github.com/golang/mock v1.6.0 + github.com/google/uuid v1.6.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.3 github.com/judwhite/go-svc v1.2.1 github.com/panjf2000/gnet/v2 v2.9.7 github.com/prometheus/client_golang v1.20.5 - github.com/redis/go-redis/v9 v9.10.0 github.com/stretchr/testify v1.11.1 go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.64.0 go.uber.org/zap v1.27.0 @@ -58,7 +58,6 @@ require ( github.com/golang-jwt/jwt/v5 v5.3.0 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/golang/snappy v0.0.4 // indirect - github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect @@ -86,6 +85,7 @@ require ( github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.57.1 // indirect github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // indirect + github.com/redis/go-redis/v9 v9.10.0 // indirect github.com/sagikazarmark/locafero v0.11.0 // indirect github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/spf13/afero v1.15.0 // indirect diff --git a/go.sum b/go.sum index e4a539e..78908b0 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI= bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed h1:O08p0egfekFqQSnc4sfEJUTI5dGiEyiDRNW/VYa/Ce4= -git.hlsq.asia/mmorpg/service-common v0.0.0-20260130035320-5dc5391b07ed/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260206142243-44ebbe444953 h1:eqGH4SIEgwY7uVIiZrMwitHQEVPetDH9SFzWwIrtWzM= +git.hlsq.asia/mmorpg/service-common v0.0.0-20260206142243-44ebbe444953/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE= diff --git a/internal/global/global.go b/internal/global/global.go index 66bf7d0..2c73ad1 100644 --- a/internal/global/global.go +++ b/internal/global/global.go @@ -13,8 +13,9 @@ const ( var GatewaySID int64 const ( - KeyGatewayAccessToken = "gateway:token:access:%v" - KeyGatewayRefreshToken = "gateway:token:refresh:%v" + KeyGatewayAccessToken = "gateway:token:access:" + KeyGatewayRefreshToken = "gateway:token:refresh:" + KeyGatewaySession = "gateway:session:" KeyGatewayInfo = "gateway:info:%v" HFieldInfoGatewaySID = "gateway_sid" diff --git a/internal/handler/http_handler/login.go b/internal/handler/http_handler/login.go index 8410095..2c6de39 100644 --- a/internal/handler/http_handler/login.go +++ b/internal/handler/http_handler/login.go @@ -1,18 +1,14 @@ package http_handler import ( - "context" - "fmt" "git.hlsq.asia/mmorpg/service-common/db/redis" "git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client" "git.hlsq.asia/mmorpg/service-common/net/http/http_resp" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "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-gonic/gin" - "time" ) // 这个模块处理用户登录 @@ -71,7 +67,12 @@ func Login(c *gin.Context) { return } - at, rt, err := genToken(c, usn) + at, rt, err := sessionLogin(c, usn) + if err != nil { + log.Errorf("Login sessionLogin error: %v, usn: %v", err, usn) + http_resp.JsonOK(c, http_resp.Error(http_resp.Failed)) + return + } http_resp.JsonOK(c, http_resp.Success(&LoginResp{ USN: usn, Name: name, @@ -81,7 +82,7 @@ func Login(c *gin.Context) { } type RefreshTokenReq struct { - RefreshToken string `json:"refreshToken" binding:"required,min=1"` + RefreshToken string `json:"refreshToken"` } type RefreshTokenResp struct { @@ -95,18 +96,27 @@ func RefreshToken(c *gin.Context) { http_resp.JsonBadRequest(c) return } - claims, err := utils.ParseToken(req.RefreshToken, config.Get().Auth.Secret) - if err != nil { + if req.RefreshToken == "" { + cookie, err := c.Cookie("refresh_token") + if err != nil { + http_resp.JsonUnauthorized(c) + return + } + req.RefreshToken = cookie + } + + usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64() + if usn == 0 { http_resp.JsonUnauthorized(c) return } - if redis.GetClient().Get(c, fmt.Sprintf(global.KeyGatewayRefreshToken, claims.USN)).Val() != req.RefreshToken { - http_resp.JsonUnauthorized(c) - return + + if err := sessionLogout(c, req.RefreshToken); err != nil { + log.Errorf("RefreshToken sessionLogout error: %v, usn: %v", err, usn) } - at, rt, err := genToken(c, claims.USN) + at, rt, err := sessionLogin(c, usn) if err != nil { - log.Errorf("RefreshToken genToken error: %v, usn: %v", err, claims.USN) + log.Errorf("RefreshToken sessionLogin error: %v, usn: %v", err, usn) http_resp.JsonOK(c, http_resp.Error(http_resp.Failed)) return } @@ -117,23 +127,29 @@ func RefreshToken(c *gin.Context) { })) } -func genToken(ctx context.Context, usn int64) (string, string, error) { - at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute) - if err != nil { - return "", "", err - } - rt, err := genTokenOne(ctx, global.KeyGatewayRefreshToken, usn, time.Duration(config.Get().Auth.LongExpire)*time.Minute) - if err != nil { - return "", "", err - } - return at, rt, nil +type LogoutReq struct { + RefreshToken string `json:"refreshToken"` } -func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) { - token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl) - if err != nil { - return "", err - } - redis.GetClient().Set(ctx, fmt.Sprintf(key, usn), token, ttl) - return token, err +type LogoutResp struct { +} + +func Logout(c *gin.Context) { + req := &LogoutReq{} + if err := c.ShouldBindJSON(req); err != nil { + http_resp.JsonBadRequest(c) + return + } + if req.RefreshToken == "" { + cookie, err := c.Cookie("refresh_token") + if err != nil { + http_resp.JsonUnauthorized(c) + return + } + req.RefreshToken = cookie + } + if err := sessionLogout(c, req.RefreshToken); err != nil { + log.Errorf("Logout sessionLogout error: %v", err) + } + http_resp.JsonOK(c, http_resp.Success(&LogoutResp{})) } diff --git a/internal/handler/http_handler/login_test.go b/internal/handler/http_handler/login_test.go index 65ce79b..483d432 100644 --- a/internal/handler/http_handler/login_test.go +++ b/internal/handler/http_handler/login_test.go @@ -2,23 +2,18 @@ package http_handler import ( "bou.ke/monkey" - "context" "fmt" - "git.hlsq.asia/mmorpg/service-common/db/redis" "git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client" "git.hlsq.asia/mmorpg/service-common/net/http/http_resp" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks" "git.hlsq.asia/mmorpg/service-common/utils" - "git.hlsq.asia/mmorpg/service-gateway/internal/global" "git.hlsq.asia/mmorpg/service-gateway/internal/testutil" "github.com/gin-gonic/gin" "github.com/golang/mock/gomock" - redis2 "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "net/http" - "strconv" "testing" ) @@ -26,17 +21,6 @@ type LoginTestSuite struct { testutil.TestSuite } -func (ts *LoginTestSuite) TestGenToken() { - usn := utils.RandInt(1, 100000000) - at, rt, err := genToken(context.Background(), strconv.Itoa(usn)) - ts.Assert().NoError(err) - - redisAt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayAccessToken, usn)).Val() - ts.Assert().Equal(at, redisAt) - redisRt := redis.GetClient().Get(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, usn)).Val() - ts.Assert().Equal(rt, redisRt) -} - func (ts *LoginTestSuite) TestLogin() { gin.SetMode(gin.TestMode) @@ -90,7 +74,7 @@ func (ts *LoginTestSuite) TestLogin() { client := mocks.NewMockUserClient(ctrl) client.EXPECT(). PhoneLogin(gomock.Any(), gomock.Any()). - Return(&grpc_pb.PhoneLoginResp{USN: "1", Name: "hh"}, nil) + Return(&grpc_pb.PhoneLoginResp{USN: 1, Name: "hh"}, nil) monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) { return client, nil @@ -130,41 +114,41 @@ func (ts *LoginTestSuite) TestRefreshToken() { utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid) }) - ts.Run("Redis Get Failed", func() { - monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { - if tokenString == "abc" { - return &utils.Claims{USN: "1"}, nil - } - return nil, assert.AnError - }) - defer monkey.Unpatch(utils.ParseToken) - - redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "ab", redis2.KeepTTL) - - w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ - RefreshToken: "abc", - }) - RefreshToken(c) - utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid) - }) - - ts.Run("OK", func() { - monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { - if tokenString == "abc" { - return &utils.Claims{USN: "1"}, nil - } - return nil, assert.AnError - }) - defer monkey.Unpatch(utils.ParseToken) - - redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL) - - w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ - RefreshToken: "abc", - }) - RefreshToken(c) - utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK) - }) + //ts.Run("Redis Get Failed", func() { + // monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { + // if tokenString == "abc" { + // return &utils.Claims{USN: 1}, nil + // } + // return nil, assert.AnError + // }) + // defer monkey.Unpatch(utils.ParseToken) + // + // redis.GetClient().Set(context.Background(), global.KeyGatewayRefreshToken+1, "ab", redis2.KeepTTL) + // + // w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ + // RefreshToken: "abc", + // }) + // RefreshToken(c) + // utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid) + //}) + // + //ts.Run("OK", func() { + // monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { + // if tokenString == "abc" { + // return &utils.Claims{USN: 1}, nil + // } + // return nil, assert.AnError + // }) + // defer monkey.Unpatch(utils.ParseToken) + // + // redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL) + // + // w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ + // RefreshToken: "abc", + // }) + // RefreshToken(c) + // utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK) + //}) } func TestLoginTestSuite(t *testing.T) { diff --git a/internal/handler/http_handler/session.go b/internal/handler/http_handler/session.go new file mode 100644 index 0000000..3eac305 --- /dev/null +++ b/internal/handler/http_handler/session.go @@ -0,0 +1,108 @@ +package http_handler + +import ( + "git.hlsq.asia/mmorpg/service-common/db/redis" + "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-gonic/gin" + "github.com/google/uuid" +) + +var ( + scriptLoginSha1 string + scriptLogin = ` +local at_key = KEYS[1] .. ARGV[1] +local rt_key = KEYS[2] .. ARGV[2] +local session_key = KEYS[3] .. ARGV[3] + +redis.call("HSET", at_key, + "usn", ARGV[3], + "ip", ARGV[6], + "ua", ARGV[7], + "rt", ARGV[2] +) +redis.call("EXPIRE", at_key, tonumber(ARGV[4])) + +redis.call("HSET", rt_key, + "usn", ARGV[3], + "ip", ARGV[6], + "ua", ARGV[7], + "at", ARGV[1] +) +redis.call("EXPIRE", rt_key, tonumber(ARGV[5])) + +local all_rts = redis.call("SMEMBERS", session_key) +-- 只允许最大10个会话,多了的随机踢掉 +if #all_rts >= 10 then + local rk = KEYS[2] .. all_rts[1] + local ak = KEYS[1] .. redis.call("HGET", rk, "at") + redis.call("DEL", rk) + redis.call("DEL", ak) +end +-- 清理已经失效的会话 +for i, rt in ipairs(all_rts) do + if redis.call("EXISTS", KEYS[2] .. rt) == 0 then + redis.call("SREM", session_key, rt) + end +end + +redis.call("SADD", session_key, ARGV[2]) +redis.call("EXPIRE", session_key, tonumber(ARGV[5]) + 600) + +return 1 +` +) + +func sessionLogin(c *gin.Context, usn int64) (string, string, error) { + if scriptLoginSha1 == "" { + scriptLoginSha1 = redis.GetClient().ScriptLoad(c, scriptLogin).Val() + } + at := uuid.New().String() + rt := uuid.New().String() + err := redis.GetClient().EvalSha( + c, scriptLoginSha1, + []string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession}, + at, + rt, + usn, + config.Get().Auth.ShortExpire, + config.Get().Auth.LongExpire, + c.RemoteIP(), + c.GetHeader("User-Agent"), + ).Err() + if err != nil { + return "", "", utils.ErrorsWrap(err) + } + c.SetCookie("refresh_token", rt, int(config.Get().Auth.LongExpire), "/", ".hlsq.asia", true, true) + + return at, rt, nil +} + +var ( + scriptLogoutSha1 string + scriptLogout = ` +local rt_key = KEYS[2] .. ARGV[1] +local usn = redis.call("HGET", rt_key, "usn") +local at = redis.call("HGET", rt_key, "at") +local at_key = KEYS[1] .. at +local session_key = KEYS[3] .. usn + +redis.call("DEL", at_key) +redis.call("DEL", rt_key) +redis.call("SREM", session_key, ARGV[1]) + +return 1 +` +) + +func sessionLogout(c *gin.Context, rt string) error { + if scriptLogoutSha1 == "" { + scriptLogoutSha1 = redis.GetClient().ScriptLoad(c, scriptLogout).Val() + } + return redis.GetClient().EvalSha( + c, scriptLogoutSha1, + []string{global.KeyGatewayAccessToken, global.KeyGatewayRefreshToken, global.KeyGatewaySession}, + rt, + ).Err() +} diff --git a/internal/handler/ws_handler/client/client.go b/internal/handler/ws_handler/client/client.go index 657f6a5..92c88eb 100644 --- a/internal/handler/ws_handler/client/client.go +++ b/internal/handler/ws_handler/client/client.go @@ -29,11 +29,10 @@ type Client struct { UniqueNo int64 // 副本唯一编号 } -func NewClient(usn int64, conn socket.ISocketConn) *Client { +func NewClient(conn socket.ISocketConn) *Client { client := &Client{ - USN: usn, conn: conn, - logger: log.GetLogger().Named(fmt.Sprintf("usn:%v", usn)), + logger: log.GetLogger(), heartBeat: time.Now(), mailChan: make(chan Event, 1024), } @@ -43,6 +42,11 @@ func NewClient(usn int64, conn socket.ISocketConn) *Client { return client } +func (c *Client) SetUSN(usn int64) { + c.USN = usn + c.logger = log.GetLogger().Named(fmt.Sprintf("usn:%v", usn)) +} + func (c *Client) Loop() { defer func() { if err := recover(); err != nil { diff --git a/internal/handler/ws_handler/login/login.go b/internal/handler/ws_handler/login/login.go index fd9d554..14d7881 100644 --- a/internal/handler/ws_handler/login/login.go +++ b/internal/handler/ws_handler/login/login.go @@ -8,6 +8,7 @@ import ( "git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb" + "git.hlsq.asia/mmorpg/service-common/utils" "git.hlsq.asia/mmorpg/service-gateway/internal/global" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client" "sync" @@ -152,7 +153,9 @@ func (l *Login) StartLogin(user *User) { // CheckToken 校验Token是否有效 func (l *Login) CheckToken(user *User) bool { - return redis.GetClient().Get(l.ctx, fmt.Sprintf(global.KeyGatewayAccessToken, user.Cli.USN)).Val() == user.Token + usn, _ := redis.GetClient().HGet(context.Background(), global.KeyGatewayAccessToken+user.Token, (&utils.UserSession{}).GetUsnKey()).Int64() + user.Cli.SetUSN(usn) + return usn > 0 } // CheckOnline 校验是否在线 diff --git a/internal/net/http_gateway/middleward.go b/internal/net/http_gateway/middleward.go index e0376f0..8a7965f 100644 --- a/internal/net/http_gateway/middleward.go +++ b/internal/net/http_gateway/middleward.go @@ -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() } } diff --git a/internal/net/http_gateway/router.go b/internal/net/http_gateway/router.go index 8a1a54c..21a7f0d 100644 --- a/internal/net/http_gateway/router.go +++ b/internal/net/http_gateway/router.go @@ -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) diff --git a/internal/net/ws_gateway/server.go b/internal/net/ws_gateway/server.go index 774c4d6..4206e44 100644 --- a/internal/net/ws_gateway/server.go +++ b/internal/net/ws_gateway/server.go @@ -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 diff --git a/internal/testutil/suite.go b/internal/testutil/suite.go index 4fb8a6d..2ebb428 100644 --- a/internal/testutil/suite.go +++ b/internal/testutil/suite.go @@ -28,7 +28,6 @@ func (ts *TestSuite) SetupSuite() { }, }, Auth: &config.AuthConfig{ - Secret: "test", ShortExpire: 15, LongExpire: 10080, },