Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb8e9bad4b | ||
|
|
1005e89e4f | ||
|
|
268c7f99c7 | ||
| fc72fd123d | |||
| 63a4653eb2 |
@@ -4,20 +4,20 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.apinb.com/bsm-sdk/core/cache/redis"
|
|
||||||
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
"git.apinb.com/bsm-sdk/core/crypto/encipher"
|
||||||
"git.apinb.com/bsm-sdk/core/errcode"
|
"git.apinb.com/bsm-sdk/core/errcode"
|
||||||
"git.apinb.com/bsm-sdk/core/types"
|
"git.apinb.com/bsm-sdk/core/types"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func JwtAuth(redis *redis.RedisClient) gin.HandlerFunc {
|
func JwtAuth(time_verify bool) gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
// 从请求头中获取 Authorization
|
// 从请求头中获取 Authorization
|
||||||
authHeader := c.GetHeader("Authorization")
|
authHeader := c.GetHeader("Authorization")
|
||||||
if authHeader == "" {
|
if authHeader == "" {
|
||||||
log.Println("获取token异常:", "Authorization header is required")
|
log.Printf("获取token异常:%v\n", "Authorization header is required")
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Authorization header is required"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
@@ -25,21 +25,22 @@ func JwtAuth(redis *redis.RedisClient) gin.HandlerFunc {
|
|||||||
// 提取Token
|
// 提取Token
|
||||||
claims, err := encipher.ParseTokenAes(authHeader)
|
claims, err := encipher.ParseTokenAes(authHeader)
|
||||||
if err != nil || claims == nil {
|
if err != nil || claims == nil {
|
||||||
log.Println("提取token异常:", "Token is required")
|
log.Printf("提取token异常:%v\n", err)
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is required"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token is required"})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从redis 获取token,判断当前redis 是否为空
|
// 检测是否需要验证token时间
|
||||||
// tokenKey := fmt.Sprintf("%d-%s-%s", claims.ID, claims.Role, "token")
|
if time_verify {
|
||||||
// redisToken := redis.Client.Get(redis.Ctx, tokenKey)
|
// 判断时间claims.ExpiresAt
|
||||||
// if redisToken.Val() == "" {
|
if time.Now().Unix() > claims.ExpiresAt {
|
||||||
// log.Println("redis异常", "Token status unauthorized")
|
log.Println("token过期,请重新获取:", "Token has expired")
|
||||||
// c.JSON(http.StatusUnauthorized, gin.H{"error": "Token status unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"error": "Token has expired"})
|
||||||
// c.Abort()
|
c.Abort()
|
||||||
// return
|
return
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 将解析后的 Token 存储到上下文中
|
// 将解析后的 Token 存储到上下文中
|
||||||
c.Set("Auth", claims)
|
c.Set("Auth", claims)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ type LogItem struct {
|
|||||||
Level uint `json:"level"`
|
Level uint `json:"level"`
|
||||||
Ip string `json:"ip"`
|
Ip string `json:"ip"`
|
||||||
Module string `json:"module"`
|
Module string `json:"module"`
|
||||||
|
Encry bool `json:"encry"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
14
utils/ext.go
14
utils/ext.go
@@ -1,6 +1,10 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@@ -12,7 +16,7 @@ func If(condition bool, trueValue, falseValue interface{}) interface{} {
|
|||||||
return falseValue
|
return falseValue
|
||||||
}
|
}
|
||||||
|
|
||||||
//如果首字母是小写字母, 则变换为大写字母
|
// 如果首字母是小写字母, 则变换为大写字母
|
||||||
func FirstToUpper(str string) string {
|
func FirstToUpper(str string) string {
|
||||||
if str == "" {
|
if str == "" {
|
||||||
return ""
|
return ""
|
||||||
@@ -33,3 +37,11 @@ func ParseParams(in map[string]string) map[string]interface{} {
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PrintJson(v any) {
|
||||||
|
jsonBy, _ := json.Marshal(v)
|
||||||
|
var out bytes.Buffer
|
||||||
|
json.Indent(&out, jsonBy, "", "\t")
|
||||||
|
out.WriteTo(os.Stdout)
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -21,6 +22,32 @@ func StringToFile(path, content string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件复制
|
||||||
|
func CopyFile(src, dst string) (int64, error) {
|
||||||
|
sourceFileStat, err := os.Stat(src)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
if !sourceFileStat.Mode().IsRegular() {
|
||||||
|
return 0, fmt.Errorf("%s is not a regular file", src)
|
||||||
|
}
|
||||||
|
|
||||||
|
source, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer source.Close()
|
||||||
|
|
||||||
|
destination, err := os.Create(dst)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer destination.Close()
|
||||||
|
|
||||||
|
nBytes, err := io.Copy(destination, source)
|
||||||
|
return nBytes, err
|
||||||
|
}
|
||||||
|
|
||||||
// 递归遍历文件夹
|
// 递归遍历文件夹
|
||||||
// rootDir: 文件夹根目录
|
// rootDir: 文件夹根目录
|
||||||
// s: 存储文件名的切片
|
// s: 存储文件名的切片
|
||||||
|
|||||||
Reference in New Issue
Block a user