作者 yangfu

feat: logout current user when token changed

@@ -19,10 +19,16 @@ type CreateFileCommand struct { @@ -19,10 +19,16 @@ type CreateFileCommand struct {
19 FileSize int `cname:"文件大小" json:"fileSize" valid:"Required"` 19 FileSize int `cname:"文件大小" json:"fileSize" valid:"Required"`
20 } 20 }
21 21
  22 +var MaxFileSize = 50 * 1024 * 1024
  23 +
22 func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validation) { 24 func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validation) {
23 ext := filepath.Ext(createFileCommand.Name) 25 ext := filepath.Ext(createFileCommand.Name)
24 if !(ext == domain.XLS || ext == domain.XLSX) { 26 if !(ext == domain.XLS || ext == domain.XLSX) {
25 - validation.Error(fmt.Sprintf("仅支持文件格式 xls 、 xlsx")) 27 + validation.Error("仅支持文件格式 xls 、 xlsx")
  28 + return
  29 + }
  30 + if createFileCommand.FileSize > 0 && createFileCommand.FileSize > MaxFileSize {
  31 + validation.Error("文件大小超过50M")
26 return 32 return
27 } 33 }
28 } 34 }
@@ -20,9 +20,9 @@ type TablePreviewCommand struct { @@ -20,9 +20,9 @@ type TablePreviewCommand struct {
20 } 20 }
21 21
22 func (cmd *TablePreviewCommand) Valid(validation *validation.Validation) { 22 func (cmd *TablePreviewCommand) Valid(validation *validation.Validation) {
23 - if cmd.UseCache && cmd.PageSize==0{ 23 + if cmd.UseCache && cmd.PageSize == 0 {
24 cmd.PageNumber = 1 24 cmd.PageNumber = 1
25 - cmd.PageSize = 10000 //默认缓存前10000条 25 + cmd.PageSize = 30000 //默认缓存前30000条
26 } 26 }
27 if cmd.PageSize > 0 { 27 if cmd.PageSize > 0 {
28 cmd.Where.PageNumber = cmd.PageNumber 28 cmd.Where.PageNumber = cmd.PageNumber
  1 +package domain
  2 +
  3 +const (
  4 + InvalidAccessToken = 901
  5 + InvalidRefreshToken = 902
  6 + InvalidSign = 903
  7 + InvalidClientId = 904
  8 + InvalidUUid = 905
  9 +)
  10 +
  11 +var CodeMsg = map[int]string{
  12 + InvalidAccessToken: "access token 过期或无效,需刷新令牌",
  13 + InvalidRefreshToken: "过期或失效,需重新进行登录认证操作", //refresh token
  14 + InvalidSign: "sign 签名无效,需重新登录手机 APP",
  15 + InvalidClientId: "client id 或 client secret 无效,需强制更新手机 APP",
  16 + InvalidUUid: "uuid 无效",
  17 +}
@@ -47,3 +47,20 @@ func (gateway *ApiAuthLib) MeInfo(param RequestUserMeQuery) (*DataUserMe, error) @@ -47,3 +47,20 @@ func (gateway *ApiAuthLib) MeInfo(param RequestUserMeQuery) (*DataUserMe, error)
47 } 47 }
48 return &data, nil 48 return &data, nil
49 } 49 }
  50 +
  51 +func (gateway *ApiAuthLib) LoginCheck(param RequestLoginCheck) (*DataLoginCheck, error) {
  52 + url := gateway.Host() + "/v1/login/check?token=" + param.Token
  53 + method := "get"
  54 + var data DataLoginCheck
  55 + err := gateway.FastDoRequest(url, method, param, &data, api.WithHeader(gateway.DefaultHeader()))
  56 + if errCodeMsg, ok := err.(api.ErrCodeMsg); ok {
  57 + return &DataLoginCheck{
  58 + Code: errCodeMsg.Code,
  59 + Msg: errCodeMsg.Msg,
  60 + }, nil
  61 + }
  62 + if err != nil {
  63 + return nil, err
  64 + }
  65 + return &data, nil
  66 +}
@@ -42,3 +42,11 @@ type DataUserMe struct { @@ -42,3 +42,11 @@ type DataUserMe struct {
42 Types string `json:"types"` 42 Types string `json:"types"`
43 } `json:"menus"` 43 } `json:"menus"`
44 } 44 }
  45 +
  46 +type RequestLoginCheck struct {
  47 + Token string
  48 +}
  49 +type DataLoginCheck struct {
  50 + Code int `json:"code"`
  51 + Msg string `json:"msg"`
  52 +}
@@ -8,6 +8,7 @@ import ( @@ -8,6 +8,7 @@ import (
8 "github.com/linmadan/egglib-go/web/beego/filters" 8 "github.com/linmadan/egglib-go/web/beego/filters"
9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant" 9 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain" 10 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib"
11 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers" 12 "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers"
12 "net/http" 13 "net/http"
13 "os" 14 "os"
@@ -76,6 +77,9 @@ func CreateRequestLogFilter(console bool) func(ctx *context.Context) { @@ -76,6 +77,9 @@ func CreateRequestLogFilter(console bool) func(ctx *context.Context) {
76 } 77 }
77 78
78 func JwtFilter() func(ctx *context.Context) { 79 func JwtFilter() func(ctx *context.Context) {
  80 + authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST)
  81 + authLib.BaseServiceGateway.ConnectTimeout = 200 * time.Millisecond
  82 + authLib.BaseServiceGateway.ReadWriteTimeout = 200 * time.Millisecond
79 return func(ctx *context.Context) { 83 return func(ctx *context.Context) {
80 //token := ctx.Request.Header.Get("Authorization") 84 //token := ctx.Request.Header.Get("Authorization")
81 token := ctx.Request.Header.Get("x-mmm-accesstoken") 85 token := ctx.Request.Header.Get("x-mmm-accesstoken")
@@ -85,17 +89,35 @@ func JwtFilter() func(ctx *context.Context) { @@ -85,17 +89,35 @@ func JwtFilter() func(ctx *context.Context) {
85 err := userToken.ParseToken(token) 89 err := userToken.ParseToken(token)
86 if err != nil { 90 if err != nil {
87 ctx.Output.SetStatus(http.StatusOK) 91 ctx.Output.SetStatus(http.StatusOK)
88 - ctx.Output.JSON(map[string]interface{}{  
89 - "msg": "token 过期或无效,需刷新令牌",  
90 - "code": 901,  
91 - "data": struct{}{},  
92 - }, false, false) 92 + ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidRefreshToken), false, false)
93 return 93 return
94 } 94 }
  95 + if userToken.UserId > 0 && userToken.CompanyId > 0 {
  96 + loginCheckResponse, _ := authLib.LoginCheck(authlib.RequestLoginCheck{Token: token})
  97 + if loginCheckResponse != nil && loginCheckResponse.Code == 901 {
  98 + ctx.Output.SetStatus(http.StatusOK)
  99 + ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidRefreshToken), false, false)
  100 + return
  101 + }
  102 + }
95 ctx.Input.SetData("UserToken", userToken) 103 ctx.Input.SetData("UserToken", userToken)
  104 + ctx.Input.SetData("Accesstoken", token)
96 } 105 }
97 } 106 }
98 } 107 }
  108 +
  109 +func WithCodeMsgResponse(code int) map[string]interface{} {
  110 + msg := "token 过期或无效,需刷新令牌"
  111 + if codeMsg, ok := domain.CodeMsg[code]; ok {
  112 + msg = codeMsg
  113 + }
  114 + return map[string]interface{}{
  115 + "msg": msg,
  116 + "code": code,
  117 + "data": struct{}{},
  118 + }
  119 +}
  120 +
99 func RequestCostBefore() func(ctx *context.Context) { 121 func RequestCostBefore() func(ctx *context.Context) {
100 return func(ctx *context.Context) { 122 return func(ctx *context.Context) {
101 ctx.Input.SetData("cost-begin", time.Now().UnixMilli()) 123 ctx.Input.SetData("cost-begin", time.Now().UnixMilli())
@@ -57,7 +57,11 @@ func ParseContext(c beego.BaseController) *domain.Context { @@ -57,7 +57,11 @@ func ParseContext(c beego.BaseController) *domain.Context {
57 v := cacheItem.(*authlib.DataUserMe) 57 v := cacheItem.(*authlib.DataUserMe)
58 userName = v.User.NickName 58 userName = v.User.NickName
59 } else { 59 } else {
60 - requestToken, _ := userToken.GenerateToken() 60 + //requestToken, _ := userToken.GenerateToken()
  61 + requestToken, ok := c.Ctx.Input.GetData("Accesstoken").(string)
  62 + if !ok {
  63 + goto END
  64 + }
61 authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST).WithToken(requestToken) 65 authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST).WithToken(requestToken)
62 userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{ 66 userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{
63 UserId: int(userToken.UserId), 67 UserId: int(userToken.UserId),