1
This commit is contained in:
@@ -31,6 +31,11 @@ func GenerateToken(secret string, uid int64, role string, expireSeconds int) (st
|
||||
return claims.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
// Secret 鉴权密钥(config.yml auth.secret),token 签发与解析共用入口。
|
||||
func Secret(ctx context.Context) string {
|
||||
return g.Cfg().MustGet(ctx, "auth.secret").String()
|
||||
}
|
||||
|
||||
// ParseToken 校验并解析 token,返回 uid 与 role。
|
||||
func ParseToken(secret, tokenString string) (uid int64, role string, err error) {
|
||||
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (any, error) {
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
// MiddlewareNoiseSilence 静默浏览器/DevTools 噪音探测路径(favicon、well-known),避免访问日志刷 404
|
||||
func MiddlewareNoiseSilence(r *ghttp.Request) {
|
||||
p := r.URL.Path
|
||||
if p == "/favicon.ico" || strings.HasPrefix(p, "/.well-known/") {
|
||||
r.Response.WriteHeader(http.StatusNoContent)
|
||||
r.Exit()
|
||||
return
|
||||
}
|
||||
r.Middleware.Next()
|
||||
}
|
||||
Reference in New Issue
Block a user