作者 yangfu

feat: logout current user when token changed

... ... @@ -19,10 +19,16 @@ type CreateFileCommand struct {
FileSize int `cname:"文件大小" json:"fileSize" valid:"Required"`
}
var MaxFileSize = 50 * 1024 * 1024
func (createFileCommand *CreateFileCommand) Valid(validation *validation.Validation) {
ext := filepath.Ext(createFileCommand.Name)
if !(ext == domain.XLS || ext == domain.XLSX) {
validation.Error(fmt.Sprintf("仅支持文件格式 xls 、 xlsx"))
validation.Error("仅支持文件格式 xls 、 xlsx")
return
}
if createFileCommand.FileSize > 0 && createFileCommand.FileSize > MaxFileSize {
validation.Error("文件大小超过50M")
return
}
}
... ...
... ... @@ -20,9 +20,9 @@ type TablePreviewCommand struct {
}
func (cmd *TablePreviewCommand) Valid(validation *validation.Validation) {
if cmd.UseCache && cmd.PageSize==0{
if cmd.UseCache && cmd.PageSize == 0 {
cmd.PageNumber = 1
cmd.PageSize = 10000 //默认缓存前10000条
cmd.PageSize = 30000 //默认缓存前30000条
}
if cmd.PageSize > 0 {
cmd.Where.PageNumber = cmd.PageNumber
... ...
package domain
const (
InvalidAccessToken = 901
InvalidRefreshToken = 902
InvalidSign = 903
InvalidClientId = 904
InvalidUUid = 905
)
var CodeMsg = map[int]string{
InvalidAccessToken: "access token 过期或无效,需刷新令牌",
InvalidRefreshToken: "过期或失效,需重新进行登录认证操作", //refresh token
InvalidSign: "sign 签名无效,需重新登录手机 APP",
InvalidClientId: "client id 或 client secret 无效,需强制更新手机 APP",
InvalidUUid: "uuid 无效",
}
... ...
... ... @@ -47,3 +47,20 @@ func (gateway *ApiAuthLib) MeInfo(param RequestUserMeQuery) (*DataUserMe, error)
}
return &data, nil
}
func (gateway *ApiAuthLib) LoginCheck(param RequestLoginCheck) (*DataLoginCheck, error) {
url := gateway.Host() + "/v1/login/check?token=" + param.Token
method := "get"
var data DataLoginCheck
err := gateway.FastDoRequest(url, method, param, &data, api.WithHeader(gateway.DefaultHeader()))
if errCodeMsg, ok := err.(api.ErrCodeMsg); ok {
return &DataLoginCheck{
Code: errCodeMsg.Code,
Msg: errCodeMsg.Msg,
}, nil
}
if err != nil {
return nil, err
}
return &data, nil
}
... ...
... ... @@ -42,3 +42,11 @@ type DataUserMe struct {
Types string `json:"types"`
} `json:"menus"`
}
type RequestLoginCheck struct {
Token string
}
type DataLoginCheck struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
... ...
... ... @@ -8,6 +8,7 @@ import (
"github.com/linmadan/egglib-go/web/beego/filters"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/api/authlib"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/port/beego/controllers"
"net/http"
"os"
... ... @@ -76,6 +77,9 @@ func CreateRequestLogFilter(console bool) func(ctx *context.Context) {
}
func JwtFilter() func(ctx *context.Context) {
authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST)
authLib.BaseServiceGateway.ConnectTimeout = 200 * time.Millisecond
authLib.BaseServiceGateway.ReadWriteTimeout = 200 * time.Millisecond
return func(ctx *context.Context) {
//token := ctx.Request.Header.Get("Authorization")
token := ctx.Request.Header.Get("x-mmm-accesstoken")
... ... @@ -85,17 +89,35 @@ func JwtFilter() func(ctx *context.Context) {
err := userToken.ParseToken(token)
if err != nil {
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(map[string]interface{}{
"msg": "token 过期或无效,需刷新令牌",
"code": 901,
"data": struct{}{},
}, false, false)
ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidRefreshToken), false, false)
return
}
if userToken.UserId > 0 && userToken.CompanyId > 0 {
loginCheckResponse, _ := authLib.LoginCheck(authlib.RequestLoginCheck{Token: token})
if loginCheckResponse != nil && loginCheckResponse.Code == 901 {
ctx.Output.SetStatus(http.StatusOK)
ctx.Output.JSON(WithCodeMsgResponse(domain.InvalidRefreshToken), false, false)
return
}
}
ctx.Input.SetData("UserToken", userToken)
ctx.Input.SetData("Accesstoken", token)
}
}
}
func WithCodeMsgResponse(code int) map[string]interface{} {
msg := "token 过期或无效,需刷新令牌"
if codeMsg, ok := domain.CodeMsg[code]; ok {
msg = codeMsg
}
return map[string]interface{}{
"msg": msg,
"code": code,
"data": struct{}{},
}
}
func RequestCostBefore() func(ctx *context.Context) {
return func(ctx *context.Context) {
ctx.Input.SetData("cost-begin", time.Now().UnixMilli())
... ...
... ... @@ -57,7 +57,11 @@ func ParseContext(c beego.BaseController) *domain.Context {
v := cacheItem.(*authlib.DataUserMe)
userName = v.User.NickName
} else {
requestToken, _ := userToken.GenerateToken()
//requestToken, _ := userToken.GenerateToken()
requestToken, ok := c.Ctx.Input.GetData("Accesstoken").(string)
if !ok {
goto END
}
authLib := authlib.NewApiAuthLib(constant.AUTH_SERVER_HOST).WithToken(requestToken)
userInfo, err := authLib.MeInfo(authlib.RequestUserMeQuery{
UserId: int(userToken.UserId),
... ...