-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
499e171
commit 5d79750
Showing
21 changed files
with
317 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/xinliangnote/go-gin-api/configs" | ||
"github.com/xinliangnote/go-gin-api/internal/api/code" | ||
"github.com/xinliangnote/go-gin-api/internal/pkg/cache" | ||
"github.com/xinliangnote/go-gin-api/internal/pkg/core" | ||
"github.com/xinliangnote/go-gin-api/pkg/errno" | ||
"github.com/xinliangnote/go-gin-api/pkg/token" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func (m *middleware) Resubmit() core.HandlerFunc { | ||
|
||
redisKeyPrefix := configs.ProjectName() + ":request-id:" | ||
|
||
return func(c core.Context) { | ||
cfg := configs.Get().URLToken | ||
|
||
tokenString, err := token.New(cfg.Secret).UrlSign(c.Path(), c.Method(), c.RequestInputParams()) | ||
if err != nil { | ||
c.AbortWithError(errno.NewError( | ||
http.StatusBadRequest, | ||
code.ResubmitError, | ||
code.Text(code.ResubmitError)).WithErr(err), | ||
) | ||
return | ||
} | ||
|
||
redisKey := redisKeyPrefix + tokenString | ||
if !m.cache.Exists(redisKey) { | ||
err = m.cache.Set(redisKey, "1", time.Minute*cfg.ExpireDuration) | ||
if err != nil { | ||
c.AbortWithError(errno.NewError( | ||
http.StatusBadRequest, | ||
code.ResubmitError, | ||
code.Text(code.ResubmitError)).WithErr(err), | ||
) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
redisValue, err := m.cache.Get(redisKey, cache.WithTrace(c.Trace())) | ||
if err != nil { | ||
c.AbortWithError(errno.NewError( | ||
http.StatusBadRequest, | ||
code.ResubmitError, | ||
code.Text(code.ResubmitError)).WithErr(err), | ||
) | ||
return | ||
} | ||
|
||
if redisValue == "1" { | ||
c.AbortWithError(errno.NewError( | ||
http.StatusBadRequest, | ||
code.ResubmitMsg, | ||
code.Text(code.ResubmitMsg)).WithErr(errors.New("resubmit")), | ||
) | ||
return | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/xinliangnote/go-gin-api/internal/api/service/user_service" | ||
"github.com/xinliangnote/go-gin-api/internal/pkg/cache" | ||
"github.com/xinliangnote/go-gin-api/internal/pkg/core" | ||
"github.com/xinliangnote/go-gin-api/internal/pkg/grpc" | ||
"github.com/xinliangnote/go-gin-api/pkg/errno" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
var _ Middleware = (*middleware)(nil) | ||
|
||
type Middleware interface { | ||
// i 为了避免被其他包实现 | ||
i() | ||
|
||
// JWT 中间件 | ||
Jwt(ctx core.Context) (userId int64, userName string, err errno.Error) | ||
|
||
// Resubmit 中间件 | ||
Resubmit() core.HandlerFunc | ||
} | ||
|
||
type middleware struct { | ||
logger *zap.Logger | ||
cache cache.Repo | ||
grpConn grpc.ClientConn | ||
userService user_service.UserService | ||
} | ||
|
||
func New(logger *zap.Logger, cache cache.Repo) Middleware { | ||
return &middleware{ | ||
logger: logger, | ||
cache: cache, | ||
} | ||
} | ||
|
||
func (m *middleware) i() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
## 与 UrlSign 对应的 PHP 加密算法 | ||
|
||
```php | ||
// 对 params key 进行排序 | ||
ksort($params); | ||
|
||
// 对 sortParams 进行 Encode | ||
$sortParamsEncode = http_build_query($params); | ||
|
||
// 加密字符串规则 path + method + sortParamsEncode + secret | ||
$encryptStr = $path . $method . $sortParamsEncode . $secret | ||
|
||
// 对加密字符串进行 md5 | ||
$md5Str = md5($encryptStr); | ||
|
||
// 对 md5Str 进行 base64 encode | ||
$tokenString = base64_encode($md5Str); | ||
|
||
echo $tokenString; | ||
``` |
Oops, something went wrong.