Compare commits

..

3 Commits

Author SHA1 Message Date
b4cd51a6dc refactor(encipher): 更新 JWT 过期时间变量
- 将 vars.JwtExpireDay 更改为 vars.JwtExpire,使代码更具通用性
- 优化了 GenerateTokenAes 函数中的过期时间计算逻辑
2025-09-12 20:11:49 +08:00
dac969d798 refactor(vars): 修改 JWT 过期时间变量名称
- 将 JwtExpireDay 重命名为 JwtExpire
- 新变量名称更加简洁,同时消除了冗余的 Day 后缀
2025-09-12 18:56:23 +08:00
2f398c73b3 add middleware for CORS and mode configuration 2025-09-02 10:01:09 +08:00
4 changed files with 36 additions and 2 deletions

View File

@@ -30,7 +30,7 @@ func GenerateTokenAes(id uint, identity, client, role string, owner any, extend
if !(JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) { if !(JwtSecretLen == 16 || JwtSecretLen == 24 || JwtSecretLen == 32) {
return "", errcode.ErrJWTSecretKey return "", errcode.ErrJWTSecretKey
} }
expireTime := time.Now().Add(vars.JwtExpireDay) expireTime := time.Now().Add(vars.JwtExpire)
claims := types.JwtClaims{ claims := types.JwtClaims{
ID: id, ID: id,
Identity: identity, Identity: identity,

18
middleware/cors.go Normal file
View File

@@ -0,0 +1,18 @@
package middleware
import (
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func Cors() gin.HandlerFunc {
return cors.New(cors.Config{
AllowAllOrigins: true,
AllowHeaders: []string{
"Origin", "Content-Length", "Content-Type", "Workspace", "Request-Id", "Authorization", "Token",
},
AllowMethods: []string{
"GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS",
},
})
}

16
middleware/mode.go Normal file
View File

@@ -0,0 +1,16 @@
package middleware
import (
"git.apinb.com/bsm-sdk/core/env"
"git.apinb.com/bsm-sdk/core/vars"
"github.com/gin-gonic/gin"
)
func Mode(app *gin.Engine) {
// 设置gin模式
if env.Runtime.Mode == vars.RUN_MODE_PROD {
gin.SetMode(gin.ReleaseMode)
} else {
gin.SetMode(gin.DebugMode)
}
}

View File

@@ -4,5 +4,5 @@ import "time"
var ( var (
// cache def value // cache def value
JwtExpireDay time.Duration = 1 * 24 * time.Hour JwtExpire time.Duration = 1 * 24 * time.Hour
) )