fix: 修改包名
This commit is contained in:
parent
b89d1021c9
commit
ee1a42a106
|
@ -1,4 +1,4 @@
|
||||||
package _const
|
package constant
|
||||||
|
|
||||||
// gva 常量文件,用于定义一些常量 , 如果修改此处,请自行修改前端中的/src/const/index.js 中对应的
|
// gva 常量文件,用于定义一些常量 , 如果修改此处,请自行修改前端中的/src/const/index.js 中对应的
|
||||||
const TOKEN_NAME = "x-token" // token ,此处是存储在localStorage中的token名称
|
const TOKEN_NAME = "x-token" // token ,此处是存储在localStorage中的token名称
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package _const
|
package constant
|
||||||
|
|
||||||
const (
|
const (
|
||||||
JWT_NO_AUTH = 6
|
JWT_NO_AUTH = 6
|
||||||
|
|
|
@ -2,6 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
constant "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"io"
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
@ -25,7 +26,7 @@ func ErrorToEmail() gin.HandlerFunc {
|
||||||
if claims.Username != "" {
|
if claims.Username != "" {
|
||||||
username = claims.Username
|
username = claims.Username
|
||||||
} else {
|
} else {
|
||||||
id, _ := strconv.Atoi(c.Request.Header.Get("x-user-id"))
|
id, _ := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID))
|
||||||
user, err := userService.FindUserById(id)
|
user, err := userService.FindUserById(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
username = "Unknown"
|
username = "Unknown"
|
||||||
|
|
|
@ -2,7 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||||
"github.com/golang-jwt/jwt/v4"
|
"github.com/golang-jwt/jwt/v4"
|
||||||
|
@ -56,14 +56,14 @@ func JWTAuth() gin.HandlerFunc {
|
||||||
// response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
|
// response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
|
||||||
// c.Abort()
|
// c.Abort()
|
||||||
//}
|
//}
|
||||||
c.Set(_const.JWT_CLAIMS, claims)
|
c.Set(constant.JWT_CLAIMS, claims)
|
||||||
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
if claims.ExpiresAt.Unix()-time.Now().Unix() < claims.BufferTime {
|
||||||
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
dr, _ := utils.ParseDuration(global.GVA_CONFIG.JWT.ExpiresTime)
|
||||||
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(dr))
|
claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(dr))
|
||||||
newToken, _ := j.CreateTokenByOldToken(token, *claims)
|
newToken, _ := j.CreateTokenByOldToken(token, *claims)
|
||||||
newClaims, _ := j.ParseToken(newToken)
|
newClaims, _ := j.ParseToken(newToken)
|
||||||
c.Header(_const.NEW_TOKEN_NAME, newToken)
|
c.Header(constant.NEW_TOKEN_NAME, newToken)
|
||||||
c.Header(_const.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
c.Header(constant.NEW_TOKEN_EXPIRES_AT, strconv.FormatInt(newClaims.ExpiresAt.Unix(), 10))
|
||||||
utils.SetToken(c, newToken, int(dr.Seconds()))
|
utils.SetToken(c, newToken, int(dr.Seconds()))
|
||||||
if global.GVA_CONFIG.System.UseMultipoint {
|
if global.GVA_CONFIG.System.UseMultipoint {
|
||||||
// 记录新的活跃jwt
|
// 记录新的活跃jwt
|
||||||
|
@ -72,11 +72,11 @@ func JWTAuth() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|
||||||
if newToken, exists := c.Get(_const.NEW_TOKEN_NAME); exists {
|
if newToken, exists := c.Get(constant.NEW_TOKEN_NAME); exists {
|
||||||
c.Header(_const.NEW_TOKEN_NAME, newToken.(string))
|
c.Header(constant.NEW_TOKEN_NAME, newToken.(string))
|
||||||
}
|
}
|
||||||
if newExpiresAt, exists := c.Get(_const.NEW_TOKEN_EXPIRES_AT); exists {
|
if newExpiresAt, exists := c.Get(constant.NEW_TOKEN_EXPIRES_AT); exists {
|
||||||
c.Header(_const.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string))
|
c.Header(constant.NEW_TOKEN_EXPIRES_AT, newExpiresAt.(string))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ type LimitConfig struct {
|
||||||
func (l LimitConfig) LimitWithTime() gin.HandlerFunc {
|
func (l LimitConfig) LimitWithTime() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
if err := l.CheckOrMark(l.GenerationKey(c), l.Expire, l.Limit); err != nil {
|
if err := l.CheckOrMark(l.GenerationKey(c), l.Expire, l.Limit); err != nil {
|
||||||
c.JSON(http.StatusOK, gin.H{"code": _const.ERROR, "msg": err})
|
c.JSON(http.StatusOK, gin.H{"code": constant.ERROR, "msg": err})
|
||||||
c.Abort()
|
c.Abort()
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package middleware
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
constant "github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -60,7 +61,7 @@ func OperationRecord() gin.HandlerFunc {
|
||||||
if claims != nil && claims.BaseClaims.ID != 0 {
|
if claims != nil && claims.BaseClaims.ID != 0 {
|
||||||
userId = int(claims.BaseClaims.ID)
|
userId = int(claims.BaseClaims.ID)
|
||||||
} else {
|
} else {
|
||||||
id, err := strconv.Atoi(c.Request.Header.Get("x-user-id"))
|
id, err := strconv.Atoi(c.Request.Header.Get(constant.REQUEST_USER_ID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
userId = 0
|
userId = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package response
|
package response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
@ -23,37 +23,37 @@ func Result(code int, data interface{}, msg string, c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ok(c *gin.Context) {
|
func Ok(c *gin.Context) {
|
||||||
Result(_const.SUCCESS, map[string]interface{}{}, "操作成功", c)
|
Result(constant.SUCCESS, map[string]interface{}{}, "操作成功", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OkWithMessage(message string, c *gin.Context) {
|
func OkWithMessage(message string, c *gin.Context) {
|
||||||
Result(_const.SUCCESS, map[string]interface{}{}, message, c)
|
Result(constant.SUCCESS, map[string]interface{}{}, message, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OkWithData(data interface{}, c *gin.Context) {
|
func OkWithData(data interface{}, c *gin.Context) {
|
||||||
Result(_const.SUCCESS, data, "成功", c)
|
Result(constant.SUCCESS, data, "成功", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
func OkWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||||
Result(_const.SUCCESS, data, message, c)
|
Result(constant.SUCCESS, data, message, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Fail(c *gin.Context) {
|
func Fail(c *gin.Context) {
|
||||||
Result(_const.ERROR, map[string]interface{}{}, "操作失败", c)
|
Result(constant.ERROR, map[string]interface{}{}, "操作失败", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func FailWithMessage(message string, c *gin.Context) {
|
func FailWithMessage(message string, c *gin.Context) {
|
||||||
Result(_const.ERROR, map[string]interface{}{}, message, c)
|
Result(constant.ERROR, map[string]interface{}{}, message, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NoAuth(message string, c *gin.Context) {
|
func NoAuth(message string, c *gin.Context) {
|
||||||
c.JSON(http.StatusUnauthorized, Response{
|
c.JSON(http.StatusUnauthorized, Response{
|
||||||
_const.JWT_NO_AUTH,
|
constant.JWT_NO_AUTH,
|
||||||
nil,
|
nil,
|
||||||
message,
|
message,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
func FailWithDetailed(data interface{}, message string, c *gin.Context) {
|
||||||
Result(_const.ERROR, data, message, c)
|
Result(constant.ERROR, data, message, c)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
_const "github.com/flipped-aurora/gin-vue-admin/server/const"
|
"github.com/flipped-aurora/gin-vue-admin/server/const"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||||
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||||
|
@ -19,9 +19,9 @@ func ClearToken(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if net.ParseIP(host) != nil {
|
if net.ParseIP(host) != nil {
|
||||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false)
|
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", "", false, false)
|
||||||
} else {
|
} else {
|
||||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false)
|
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, "", -1, "/", host, false, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,17 +33,17 @@ func SetToken(c *gin.Context, token string, maxAge int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if net.ParseIP(host) != nil {
|
if net.ParseIP(host) != nil {
|
||||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false)
|
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", "", false, false)
|
||||||
} else {
|
} else {
|
||||||
c.SetCookie(_const.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false)
|
c.SetCookie(constant.REQUSET_COOKIE_TOKEN_NAME, token, maxAge, "/", host, false, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetToken(c *gin.Context) string {
|
func GetToken(c *gin.Context) string {
|
||||||
token, _ := c.Cookie(_const.REQUSET_COOKIE_TOKEN_NAME)
|
token, _ := c.Cookie(constant.REQUSET_COOKIE_TOKEN_NAME)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
j := NewJWT()
|
j := NewJWT()
|
||||||
token = c.Request.Header.Get(_const.REQUEST_HEAER_TOKEN_NAME)
|
token = c.Request.Header.Get(constant.REQUEST_HEAER_TOKEN_NAME)
|
||||||
claims, err := j.ParseToken(token)
|
claims, err := j.ParseToken(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
global.GVA_LOG.Error("重新写入cookie token失败,未能成功解析token,请检查请求头是否存在x-token且claims是否为规定结构")
|
||||||
|
@ -66,7 +66,7 @@ func GetClaims(c *gin.Context) (*systemReq.CustomClaims, error) {
|
||||||
|
|
||||||
// GetUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
// GetUserID 从Gin的Context中获取从jwt解析出来的用户ID
|
||||||
func GetUserID(c *gin.Context) uint {
|
func GetUserID(c *gin.Context) uint {
|
||||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return 0
|
return 0
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,7 +80,7 @@ func GetUserID(c *gin.Context) uint {
|
||||||
|
|
||||||
// GetUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
// GetUserUuid 从Gin的Context中获取从jwt解析出来的用户UUID
|
||||||
func GetUserUuid(c *gin.Context) uuid.UUID {
|
func GetUserUuid(c *gin.Context) uuid.UUID {
|
||||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return uuid.UUID{}
|
return uuid.UUID{}
|
||||||
} else {
|
} else {
|
||||||
|
@ -94,7 +94,7 @@ func GetUserUuid(c *gin.Context) uuid.UUID {
|
||||||
|
|
||||||
// GetUserAuthorityId 从Gin的Context中获取从jwt解析出来的用户角色id
|
// GetUserAuthorityId 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||||
func GetUserAuthorityId(c *gin.Context) uint {
|
func GetUserAuthorityId(c *gin.Context) uint {
|
||||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return 0
|
return 0
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,7 +108,7 @@ func GetUserAuthorityId(c *gin.Context) uint {
|
||||||
|
|
||||||
// GetUserInfo 从Gin的Context中获取从jwt解析出来的用户角色id
|
// GetUserInfo 从Gin的Context中获取从jwt解析出来的用户角色id
|
||||||
func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
||||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -122,7 +122,7 @@ func GetUserInfo(c *gin.Context) *systemReq.CustomClaims {
|
||||||
|
|
||||||
// GetUserName 从Gin的Context中获取从jwt解析出来的用户名
|
// GetUserName 从Gin的Context中获取从jwt解析出来的用户名
|
||||||
func GetUserName(c *gin.Context) string {
|
func GetUserName(c *gin.Context) string {
|
||||||
if claims, exists := c.Get(_const.JWT_CLAIMS); !exists {
|
if claims, exists := c.Get(constant.JWT_CLAIMS); !exists {
|
||||||
if cl, err := GetClaims(c); err != nil {
|
if cl, err := GetClaims(c); err != nil {
|
||||||
return ""
|
return ""
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue