正在显示
9 个修改的文件
包含
41 行增加
和
26 行删除
| @@ -34,7 +34,7 @@ func (this *BaseController) Prepare() { | @@ -34,7 +34,7 @@ func (this *BaseController) Prepare() { | ||
| 34 | 34 | ||
| 35 | func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) { | 35 | func (this *BaseController) GetAppHead() (appHead protocol.BaseHeader) { |
| 36 | appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken) | 36 | appHead.AccessToken = this.Ctx.Input.Header(protocol.HeaderAccessToken) |
| 37 | - appHead.RefreshToken = this.Ctx.Input.Header(protocol.HeaderRefreshToken) | 37 | + //appHead.RefreshToken = this.Ctx.Input.Header(protocol.HeaderRefreshToken) |
| 38 | return | 38 | return |
| 39 | 39 | ||
| 40 | } | 40 | } |
| @@ -18,8 +18,8 @@ import ( | @@ -18,8 +18,8 @@ import ( | ||
| 18 | var LogRequestData = func(ctx *context.Context) { | 18 | var LogRequestData = func(ctx *context.Context) { |
| 19 | log.Info("====>Recv Request:%s", ctx.Input.URI()) | 19 | log.Info("====>Recv Request:%s", ctx.Input.URI()) |
| 20 | hmap := map[string]string{ | 20 | hmap := map[string]string{ |
| 21 | - protocol.HeaderAccessToken: ctx.Input.Header(protocol.HeaderAccessToken), | ||
| 22 | - protocol.HeaderRefreshToken: ctx.Input.Header(protocol.HeaderRefreshToken), | 21 | + protocol.HeaderAccessToken: ctx.Input.Header(protocol.HeaderAccessToken), |
| 22 | + //protocol.HeaderRefreshToken: ctx.Input.Header(protocol.HeaderRefreshToken), | ||
| 23 | } | 23 | } |
| 24 | if ctx.Input.RequestBody != nil { | 24 | if ctx.Input.RequestBody != nil { |
| 25 | log.Info("====>Recv data from client:\nHeadData: %v \nBodyData: %s", hmap, string(ctx.Input.RequestBody)) | 25 | log.Info("====>Recv data from client:\nHeadData: %v \nBodyData: %s", hmap, string(ctx.Input.RequestBody)) |
| @@ -38,7 +38,7 @@ var AuthToken = func(ctx *context.Context) { | @@ -38,7 +38,7 @@ var AuthToken = func(ctx *context.Context) { | ||
| 38 | mtoken *serveauth.MyToken | 38 | mtoken *serveauth.MyToken |
| 39 | ) | 39 | ) |
| 40 | accesstoken := ctx.Input.Header(protocol.HeaderAccessToken) | 40 | accesstoken := ctx.Input.Header(protocol.HeaderAccessToken) |
| 41 | - refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken) | 41 | + // refreshToken := ctx.Input.Header(protocol.HeaderRefreshToken) |
| 42 | mtoken, err = serveauth.ValidJWTToken(accesstoken) | 42 | mtoken, err = serveauth.ValidJWTToken(accesstoken) |
| 43 | if accesstoken == "123456" && beego.BConfig.RunMode != "prod" { | 43 | if accesstoken == "123456" && beego.BConfig.RunMode != "prod" { |
| 44 | return | 44 | return |
| @@ -47,14 +47,14 @@ var AuthToken = func(ctx *context.Context) { | @@ -47,14 +47,14 @@ var AuthToken = func(ctx *context.Context) { | ||
| 47 | storetoken, err = redisdata.GetLoginToken(mtoken.UID) | 47 | storetoken, err = redisdata.GetLoginToken(mtoken.UID) |
| 48 | if err != nil { | 48 | if err != nil { |
| 49 | log.Error("redisdata.GetLoginToken err:%s", err) | 49 | log.Error("redisdata.GetLoginToken err:%s", err) |
| 50 | - msg = protocol.NewMesage("10024") | 50 | + msg = protocol.NewMessage("10024") |
| 51 | ctx.Output.JSON(msg, false, false) | 51 | ctx.Output.JSON(msg, false, false) |
| 52 | return | 52 | return |
| 53 | } | 53 | } |
| 54 | if beego.BConfig.RunMode == "prod" { | 54 | if beego.BConfig.RunMode == "prod" { |
| 55 | //校验是否是单客户端操作 | 55 | //校验是否是单客户端操作 |
| 56 | if storetoken.AccessToken != accesstoken { | 56 | if storetoken.AccessToken != accesstoken { |
| 57 | - msg = protocol.NewMesage("10025") | 57 | + msg = protocol.NewMessage("10025") |
| 58 | ctx.Output.JSON(msg, false, false) | 58 | ctx.Output.JSON(msg, false, false) |
| 59 | return | 59 | return |
| 60 | } | 60 | } |
| @@ -64,14 +64,12 @@ var AuthToken = func(ctx *context.Context) { | @@ -64,14 +64,12 @@ var AuthToken = func(ctx *context.Context) { | ||
| 64 | return | 64 | return |
| 65 | } | 65 | } |
| 66 | if ok := serveauth.IsJwtErrorExpired(err); ok { | 66 | if ok := serveauth.IsJwtErrorExpired(err); ok { |
| 67 | - //token过期,刷新 | ||
| 68 | - logintoken, err := serveauth.RefreshLoginToken(refreshToken) | ||
| 69 | - msg = protocol.NewReturnResponse(logintoken, err) | 67 | + msg := protocol.NewMessage("10024") |
| 70 | ctx.Output.JSON(msg, false, false) | 68 | ctx.Output.JSON(msg, false, false) |
| 71 | return | 69 | return |
| 72 | } | 70 | } |
| 73 | - log.Error("token 校验失败") | ||
| 74 | - msg = protocol.NewMesage("1") | 71 | + log.Error("token 校验失败:%s", err) |
| 72 | + msg = protocol.NewMessage("10024") | ||
| 75 | ctx.Output.JSON(msg, false, false) | 73 | ctx.Output.JSON(msg, false, false) |
| 76 | return | 74 | return |
| 77 | } | 75 | } |
| @@ -2,7 +2,8 @@ package protocol | @@ -2,7 +2,8 @@ package protocol | ||
| 2 | 2 | ||
| 3 | //指定的请求头字段 | 3 | //指定的请求头字段 |
| 4 | const ( | 4 | const ( |
| 5 | - HeaderAccessToken string = "x-mmm-accesstoken" | 5 | + // HeaderAccessToken string = "x-mmm-accesstoken" |
| 6 | + HeaderAccessToken string = "Authorization" | ||
| 6 | HeaderRefreshToken string = "x-mmm-refreshtoken" | 7 | HeaderRefreshToken string = "x-mmm-refreshtoken" |
| 7 | ) | 8 | ) |
| 8 | 9 |
| @@ -32,7 +32,7 @@ type ResponseMessage struct { | @@ -32,7 +32,7 @@ type ResponseMessage struct { | ||
| 32 | Data interface{} `json:"data"` | 32 | Data interface{} `json:"data"` |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | -func NewMesage(code string) *ResponseMessage { | 35 | +func NewMessage(code string) *ResponseMessage { |
| 36 | ecode := SearchErr(code) | 36 | ecode := SearchErr(code) |
| 37 | rsp := &ResponseMessage{ | 37 | rsp := &ResponseMessage{ |
| 38 | Errno: transformCode(ecode.Errno), | 38 | Errno: transformCode(ecode.Errno), |
| @@ -79,7 +79,7 @@ func (e ErrWithMessage) Unwrap() error { | @@ -79,7 +79,7 @@ func (e ErrWithMessage) Unwrap() error { | ||
| 79 | 79 | ||
| 80 | //ParseToMessage 实现CustomErrParse的接口 | 80 | //ParseToMessage 实现CustomErrParse的接口 |
| 81 | func (e ErrWithMessage) ParseToMessage() *ResponseMessage { | 81 | func (e ErrWithMessage) ParseToMessage() *ResponseMessage { |
| 82 | - return NewMesage(e.Errno) | 82 | + return NewMessage(e.Errno) |
| 83 | } | 83 | } |
| 84 | 84 | ||
| 85 | func SearchErr(code string) ErrorCode { | 85 | func SearchErr(code string) ErrorCode { |
| @@ -90,7 +90,7 @@ func SearchErr(code string) ErrorCode { | @@ -90,7 +90,7 @@ func SearchErr(code string) ErrorCode { | ||
| 90 | func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { | 90 | func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { |
| 91 | // var msg *ResponseMessage | 91 | // var msg *ResponseMessage |
| 92 | if eRR == nil { | 92 | if eRR == nil { |
| 93 | - msg = NewMesage("0") | 93 | + msg = NewMessage("0") |
| 94 | msg.Data = data | 94 | msg.Data = data |
| 95 | return msg | 95 | return msg |
| 96 | } | 96 | } |
| @@ -98,12 +98,12 @@ func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { | @@ -98,12 +98,12 @@ func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) { | ||
| 98 | return x.ParseToMessage() | 98 | return x.ParseToMessage() |
| 99 | } | 99 | } |
| 100 | 100 | ||
| 101 | - return NewMesage("1") | 101 | + return NewMessage("1") |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | //BadRequestParam 控制层响应返回 | 104 | //BadRequestParam 控制层响应返回 |
| 105 | func BadRequestParam(code string) *ResponseMessage { | 105 | func BadRequestParam(code string) *ResponseMessage { |
| 106 | - return NewMesage(code) | 106 | + return NewMessage(code) |
| 107 | } | 107 | } |
| 108 | 108 | ||
| 109 | //NewPageDataResponse 控制层分页数据响应返回 | 109 | //NewPageDataResponse 控制层分页数据响应返回 |
| @@ -112,9 +112,9 @@ func NewPageDataResponse(data interface{}, eRR error) (msg *ResponseMessage) { | @@ -112,9 +112,9 @@ func NewPageDataResponse(data interface{}, eRR error) (msg *ResponseMessage) { | ||
| 112 | if x, ok := eRR.(CustomErrParse); ok { | 112 | if x, ok := eRR.(CustomErrParse); ok { |
| 113 | return x.ParseToMessage() | 113 | return x.ParseToMessage() |
| 114 | } | 114 | } |
| 115 | - return NewMesage("1") | 115 | + return NewMessage("1") |
| 116 | } | 116 | } |
| 117 | - msg = NewMesage("0") | 117 | + msg = NewMessage("0") |
| 118 | msg.Data = map[string]interface{}{ | 118 | msg.Data = map[string]interface{}{ |
| 119 | "gridResult": data, | 119 | "gridResult": data, |
| 120 | } | 120 | } |
| @@ -6,7 +6,7 @@ import ( | @@ -6,7 +6,7 @@ import ( | ||
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | func Test_Err(t *testing.T) { | 8 | func Test_Err(t *testing.T) { |
| 9 | - errmsg := NewMesage("0") | 9 | + errmsg := NewMessage("0") |
| 10 | bt1, _ := json.Marshal(errmsg) | 10 | bt1, _ := json.Marshal(errmsg) |
| 11 | t.Log(string(bt1)) | 11 | t.Log(string(bt1)) |
| 12 | normalmsg := NewErrWithMessage("0") | 12 | normalmsg := NewErrWithMessage("0") |
| @@ -15,7 +15,7 @@ var errmessge ErrorMap = map[string]string{ | @@ -15,7 +15,7 @@ var errmessge ErrorMap = map[string]string{ | ||
| 15 | "10023": "用户无使用权限", | 15 | "10023": "用户无使用权限", |
| 16 | "10024": "登录凭证失效", | 16 | "10024": "登录凭证失效", |
| 17 | "10025": "该账号已在其他地方登录", | 17 | "10025": "该账号已在其他地方登录", |
| 18 | - "10026": "登录凭证过期", | 18 | + // "10026": "登录凭证过期", |
| 19 | "10027": "无操作权限", | 19 | "10027": "无操作权限", |
| 20 | //用户相关 | 20 | //用户相关 |
| 21 | "10031": "无效角色", | 21 | "10031": "无效角色", |
| @@ -68,9 +68,9 @@ func GenerateAuthToken(uid int64, companyid int64) (protocol.LoginAuthToken, err | @@ -68,9 +68,9 @@ func GenerateAuthToken(uid int64, companyid int64) (protocol.LoginAuthToken, err | ||
| 68 | var ( | 68 | var ( |
| 69 | authToken protocol.LoginAuthToken | 69 | authToken protocol.LoginAuthToken |
| 70 | accesstoken string //主token,请求用 | 70 | accesstoken string //主token,请求用 |
| 71 | - expiresIn int64 = 60 * 30 //主token过期时间,30分钟 | 71 | + expiresIn int64 = 60 * 60 * 2 //主token过期时间,30分钟 |
| 72 | refreshtoken string //副token,刷新主token用 | 72 | refreshtoken string //副token,刷新主token用 |
| 73 | - refreshExpires int64 = 60 * 30 * 2 //副token 过期时间 ,60分钟 | 73 | + refreshExpires int64 = 60 * 60 * 2 //副token 过期时间 ,60分钟 |
| 74 | err error | 74 | err error |
| 75 | nowtime = time.Now() | 75 | nowtime = time.Now() |
| 76 | ) | 76 | ) |
| @@ -9,5 +9,4 @@ type RedisLoginToken struct { | @@ -9,5 +9,4 @@ type RedisLoginToken struct { | ||
| 9 | AccessToken string `json:"access_token"` | 9 | AccessToken string `json:"access_token"` |
| 10 | RefreshToken string `json:"refresh_token"` | 10 | RefreshToken string `json:"refresh_token"` |
| 11 | CurrentCompany int64 `json:"current_company"` | 11 | CurrentCompany int64 `json:"current_company"` |
| 12 | - IsOk bool `json:"-"` | ||
| 13 | } | 12 | } |
| @@ -23,7 +23,6 @@ func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) | @@ -23,7 +23,6 @@ func SetLoginToken(param protocol.LoginAuthToken, userid int64, companyid int64) | ||
| 23 | AccessToken: param.AccessToken, | 23 | AccessToken: param.AccessToken, |
| 24 | RefreshToken: param.RefreshToken, | 24 | RefreshToken: param.RefreshToken, |
| 25 | CurrentCompany: companyid, | 25 | CurrentCompany: companyid, |
| 26 | - IsOk: true, | ||
| 27 | } | 26 | } |
| 28 | value, _ = json.Marshal(data) | 27 | value, _ = json.Marshal(data) |
| 29 | exp = param.RefreshExpires - nowTime | 28 | exp = param.RefreshExpires - nowTime |
| @@ -67,8 +66,26 @@ func ExistLoginToken(userid int64) bool { | @@ -67,8 +66,26 @@ func ExistLoginToken(userid int64) bool { | ||
| 67 | func DeleteLoginToken(userid int64) error { | 66 | func DeleteLoginToken(userid int64) error { |
| 68 | client := redis.GetRedis() | 67 | client := redis.GetRedis() |
| 69 | key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | 68 | key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) |
| 70 | - err:=client.Del(key).Err() | 69 | + err := client.Del(key).Err() |
| 71 | return err | 70 | return err |
| 72 | } | 71 | } |
| 73 | 72 | ||
| 73 | +// 刷新token 的有效期 | ||
| 74 | +func RefreshLoginTokenExpires(userid int64) error { | ||
| 75 | + client := redis.GetRedis() | ||
| 76 | + key := fmt.Sprintf("%s%s:%d", KEY_PREFIX, KEY_USER_TOKEN, userid) | ||
| 77 | + result, err := client.Exists(key).Result() | ||
| 78 | + if err != nil { | ||
| 79 | + return err | ||
| 80 | + } | ||
| 81 | + if result == 0 { | ||
| 82 | + return nil | ||
| 83 | + } | ||
| 84 | + _, err = client.Expire(key, 30*time.Minute).Result() | ||
| 85 | + if err != nil { | ||
| 86 | + return err | ||
| 87 | + } | ||
| 88 | + return nil | ||
| 89 | +} | ||
| 90 | + | ||
| 74 | //消息发布订阅 | 91 | //消息发布订阅 |
-
请 注册 或 登录 后发表评论