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

@@ -17,11 +17,10 @@ type Program struct {
} }
func (p *Program) Init(_ svc.Environment) error { func (p *Program) Init(_ svc.Environment) error {
base := &ModuleBase{} if err := config.LoadConfig(); err != nil {
if err := base.Init(); err != nil {
return err 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, (&module.DB{}).Bind(config.Get().DB, config.Get().App.Name))
p.moduleList = append(p.moduleList, &ModuleWebServer{}) p.moduleList = append(p.moduleList, &ModuleWebServer{})
p.moduleList = append(p.moduleList, &ModuleWebsocketServer{}) 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.Tracer{}).Bind(config.Get().Metric, common.KeyDiscoverServiceNameGateway))
p.moduleList = append(p.moduleList, &module.Discover{}) p.moduleList = append(p.moduleList, &module.Discover{})
for i, m := range p.moduleList { for _, m := range p.moduleList {
if i == 0 {
continue
}
if err := m.Init(); err != nil { if err := m.Init(); err != nil {
return err return err
} }

View File

@@ -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
}

View File

@@ -40,6 +40,5 @@ serve:
port: 18503 port: 18503
auth: auth:
secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU=" shortExpire: 3600
shortExpire: 60 longExpire: 604800
longExpire: 10080

View File

@@ -14,7 +14,6 @@ type Config struct {
} }
type AuthConfig struct { type AuthConfig struct {
Secret string `yaml:"secret"`
ShortExpire int64 `yaml:"shortExpire"` ShortExpire int64 `yaml:"shortExpire"`
LongExpire int64 `yaml:"longExpire"` LongExpire int64 `yaml:"longExpire"`
} }

View File

@@ -40,6 +40,5 @@ serve:
port: 18503 port: 18503
auth: auth:
secret: "bMa3mU4oCsX2KBex5o7GzwSnACpumFq3SdlDXYZgVTU=" shortExpire: 3600
shortExpire: 15 longExpire: 604800
longExpire: 10080

6
go.mod
View File

@@ -4,16 +4,16 @@ go 1.24.0
require ( require (
bou.ke/monkey v1.0.2 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/alicebob/miniredis/v2 v2.35.0
github.com/gin-contrib/cors v1.7.6 github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.11.0 github.com/gin-gonic/gin v1.11.0
github.com/golang/mock v1.6.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/grpc-ecosystem/grpc-gateway/v2 v2.27.3
github.com/judwhite/go-svc v1.2.1 github.com/judwhite/go-svc v1.2.1
github.com/panjf2000/gnet/v2 v2.9.7 github.com/panjf2000/gnet/v2 v2.9.7
github.com/prometheus/client_golang v1.20.5 github.com/prometheus/client_golang v1.20.5
github.com/redis/go-redis/v9 v9.10.0
github.com/stretchr/testify v1.11.1 github.com/stretchr/testify v1.11.1
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.64.0 go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.64.0
go.uber.org/zap v1.27.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-jwt/jwt/v5 v5.3.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.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/hashicorp/go-uuid v1.0.3 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/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/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.57.1 // indirect github.com/quic-go/quic-go v0.57.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 // 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/sagikazarmark/locafero v0.11.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect github.com/spf13/afero v1.15.0 // indirect

4
go.sum
View File

@@ -2,8 +2,8 @@ bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA= 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 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= 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-20260206142243-44ebbe444953 h1:eqGH4SIEgwY7uVIiZrMwitHQEVPetDH9SFzWwIrtWzM=
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/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0= github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE= github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=

View File

@@ -13,8 +13,9 @@ const (
var GatewaySID int64 var GatewaySID int64
const ( const (
KeyGatewayAccessToken = "gateway:token:access:%v" KeyGatewayAccessToken = "gateway:token:access:"
KeyGatewayRefreshToken = "gateway:token:refresh:%v" KeyGatewayRefreshToken = "gateway:token:refresh:"
KeyGatewaySession = "gateway:session:"
KeyGatewayInfo = "gateway:info:%v" KeyGatewayInfo = "gateway:info:%v"
HFieldInfoGatewaySID = "gateway_sid" HFieldInfoGatewaySID = "gateway_sid"

View File

@@ -1,18 +1,14 @@
package http_handler package http_handler
import ( import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/redis" "git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client" "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/net/http/http_resp"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/utils" "git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/config"
"git.hlsq.asia/mmorpg/service-gateway/internal/global" "git.hlsq.asia/mmorpg/service-gateway/internal/global"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"time"
) )
// 这个模块处理用户登录 // 这个模块处理用户登录
@@ -71,7 +67,12 @@ func Login(c *gin.Context) {
return 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{ http_resp.JsonOK(c, http_resp.Success(&LoginResp{
USN: usn, USN: usn,
Name: name, Name: name,
@@ -81,7 +82,7 @@ func Login(c *gin.Context) {
} }
type RefreshTokenReq struct { type RefreshTokenReq struct {
RefreshToken string `json:"refreshToken" binding:"required,min=1"` RefreshToken string `json:"refreshToken"`
} }
type RefreshTokenResp struct { type RefreshTokenResp struct {
@@ -95,18 +96,27 @@ func RefreshToken(c *gin.Context) {
http_resp.JsonBadRequest(c) http_resp.JsonBadRequest(c)
return return
} }
claims, err := utils.ParseToken(req.RefreshToken, config.Get().Auth.Secret) if req.RefreshToken == "" {
cookie, err := c.Cookie("refresh_token")
if err != nil { if err != nil {
http_resp.JsonUnauthorized(c) http_resp.JsonUnauthorized(c)
return return
} }
if redis.GetClient().Get(c, fmt.Sprintf(global.KeyGatewayRefreshToken, claims.USN)).Val() != req.RefreshToken { req.RefreshToken = cookie
}
usn, _ := redis.GetClient().HGet(c, global.KeyGatewayRefreshToken+req.RefreshToken, (&utils.UserSession{}).GetUsnKey()).Int64()
if usn == 0 {
http_resp.JsonUnauthorized(c) http_resp.JsonUnauthorized(c)
return return
} }
at, rt, err := genToken(c, claims.USN)
if err := sessionLogout(c, req.RefreshToken); err != nil {
log.Errorf("RefreshToken sessionLogout error: %v, usn: %v", err, usn)
}
at, rt, err := sessionLogin(c, usn)
if err != nil { 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)) http_resp.JsonOK(c, http_resp.Error(http_resp.Failed))
return return
} }
@@ -117,23 +127,29 @@ func RefreshToken(c *gin.Context) {
})) }))
} }
func genToken(ctx context.Context, usn int64) (string, string, error) { type LogoutReq struct {
at, err := genTokenOne(ctx, global.KeyGatewayAccessToken, usn, time.Duration(config.Get().Auth.ShortExpire)*time.Minute) RefreshToken string `json:"refreshToken"`
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
} }
func genTokenOne(ctx context.Context, key string, usn int64, ttl time.Duration) (string, error) { type LogoutResp struct {
token, err := utils.GenToken(usn, config.Get().Auth.Secret, ttl) }
if err != nil {
return "", err func Logout(c *gin.Context) {
} req := &LogoutReq{}
redis.GetClient().Set(ctx, fmt.Sprintf(key, usn), token, ttl) if err := c.ShouldBindJSON(req); err != nil {
return token, err 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{}))
} }

View File

@@ -2,23 +2,18 @@ package http_handler
import ( import (
"bou.ke/monkey" "bou.ke/monkey"
"context"
"fmt" "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/grpc/grpc_client"
"git.hlsq.asia/mmorpg/service-common/net/http/http_resp" "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"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks" "git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb/mocks"
"git.hlsq.asia/mmorpg/service-common/utils" "git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-gateway/internal/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/testutil" "git.hlsq.asia/mmorpg/service-gateway/internal/testutil"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang/mock/gomock" "github.com/golang/mock/gomock"
redis2 "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"net/http" "net/http"
"strconv"
"testing" "testing"
) )
@@ -26,17 +21,6 @@ type LoginTestSuite struct {
testutil.TestSuite 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() { func (ts *LoginTestSuite) TestLogin() {
gin.SetMode(gin.TestMode) gin.SetMode(gin.TestMode)
@@ -90,7 +74,7 @@ func (ts *LoginTestSuite) TestLogin() {
client := mocks.NewMockUserClient(ctrl) client := mocks.NewMockUserClient(ctrl)
client.EXPECT(). client.EXPECT().
PhoneLogin(gomock.Any(), gomock.Any()). 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) { monkey.Patch(grpc_client.UserNewClientLB, func() (grpc_pb.UserClient, error) {
return client, nil return client, nil
@@ -130,41 +114,41 @@ func (ts *LoginTestSuite) TestRefreshToken() {
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid) utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
}) })
ts.Run("Redis Get Failed", func() { //ts.Run("Redis Get Failed", func() {
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { // monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
if tokenString == "abc" { // if tokenString == "abc" {
return &utils.Claims{USN: "1"}, nil // return &utils.Claims{USN: 1}, nil
} // }
return nil, assert.AnError // return nil, assert.AnError
}) // })
defer monkey.Unpatch(utils.ParseToken) // defer monkey.Unpatch(utils.ParseToken)
//
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "ab", redis2.KeepTTL) // redis.GetClient().Set(context.Background(), global.KeyGatewayRefreshToken+1, "ab", redis2.KeepTTL)
//
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ // w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
RefreshToken: "abc", // RefreshToken: "abc",
}) // })
RefreshToken(c) // RefreshToken(c)
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid) // utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.TokenInvalid)
}) //})
//
ts.Run("OK", func() { //ts.Run("OK", func() {
monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) { // monkey.Patch(utils.ParseToken, func(tokenString string, secret string) (*utils.Claims, error) {
if tokenString == "abc" { // if tokenString == "abc" {
return &utils.Claims{USN: "1"}, nil // return &utils.Claims{USN: 1}, nil
} // }
return nil, assert.AnError // return nil, assert.AnError
}) // })
defer monkey.Unpatch(utils.ParseToken) // defer monkey.Unpatch(utils.ParseToken)
//
redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL) // redis.GetClient().Set(context.Background(), fmt.Sprintf(global.KeyGatewayRefreshToken, 1), "abc", redis2.KeepTTL)
//
w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{ // w, c := utils.CreateTestContext("POST", "/", &RefreshTokenReq{
RefreshToken: "abc", // RefreshToken: "abc",
}) // })
RefreshToken(c) // RefreshToken(c)
utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK) // utils.AssertResponse(&ts.Suite, w, http.StatusOK, http_resp.OK)
}) //})
} }
func TestLoginTestSuite(t *testing.T) { func TestLoginTestSuite(t *testing.T) {

View File

@@ -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()
}

View File

@@ -29,11 +29,10 @@ type Client struct {
UniqueNo int64 // 副本唯一编号 UniqueNo int64 // 副本唯一编号
} }
func NewClient(usn int64, conn socket.ISocketConn) *Client { func NewClient(conn socket.ISocketConn) *Client {
client := &Client{ client := &Client{
USN: usn,
conn: conn, conn: conn,
logger: log.GetLogger().Named(fmt.Sprintf("usn:%v", usn)), logger: log.GetLogger(),
heartBeat: time.Now(), heartBeat: time.Now(),
mailChan: make(chan Event, 1024), mailChan: make(chan Event, 1024),
} }
@@ -43,6 +42,11 @@ func NewClient(usn int64, conn socket.ISocketConn) *Client {
return 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() { func (c *Client) Loop() {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {

View File

@@ -8,6 +8,7 @@ import (
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client" "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/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_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/global"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/client"
"sync" "sync"
@@ -152,7 +153,9 @@ func (l *Login) StartLogin(user *User) {
// CheckToken 校验Token是否有效 // CheckToken 校验Token是否有效
func (l *Login) CheckToken(user *User) bool { 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 校验是否在线 // CheckOnline 校验是否在线

View File

@@ -2,9 +2,10 @@ package http_gateway
import ( import (
"fmt" "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/net/http/http_resp"
"git.hlsq.asia/mmorpg/service-common/utils" "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-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
@@ -63,17 +64,18 @@ func authJwt() gin.HandlerFunc {
c.Abort() c.Abort()
return 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) http_resp.JsonUnauthorized(c)
c.Abort() c.Abort()
return return
} }
// 这里将Header写到请求中grpc-gateway框架会读取然后传给grpc服务 // 这里将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() c.Next()
} }
} }

View File

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

View File

@@ -4,8 +4,6 @@ import (
"fmt" "fmt"
"git.hlsq.asia/mmorpg/service-common/log" "git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/socket" "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/client"
"git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login" "git.hlsq.asia/mmorpg/service-gateway/internal/handler/ws_handler/login"
"go.uber.org/zap" "go.uber.org/zap"
@@ -27,16 +25,11 @@ func (g *GatewayWsServer) OnHandShake(conn socket.ISocketConn) socket.Action {
g.logger.Warnf("token is invalid") g.logger.Warnf("token is invalid")
return socket.Close 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) conn.SetParam("client", cli)
if !login.GetLoginQueue().AddToLoginQueue(&login.User{Cli: cli, Token: token}) { 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.Close
} }
return socket.None return socket.None

View File

@@ -28,7 +28,6 @@ func (ts *TestSuite) SetupSuite() {
}, },
}, },
Auth: &config.AuthConfig{ Auth: &config.AuthConfig{
Secret: "test",
ShortExpire: 15, ShortExpire: 15,
LongExpire: 10080, LongExpire: 10080,
}, },