Browse Source

修复中间件导包错误及代码优化,更新swagger文档

main
SliverHorn 4 years ago
parent
commit
f71dc6f5d9
  1. 12
      server/api/v1/sys_authority.go
  2. 649
      server/docs/docs.go
  3. 646
      server/docs/swagger.json
  4. 512
      server/docs/swagger.yaml
  5. 4
      server/middleware/casbin_rbac.go
  6. 45
      server/middleware/jwt.go

12
server/api/v1/sys_authority.go

@ -11,7 +11,7 @@ import (
"go.uber.org/zap"
)
// @Tags authority
// @Tags Authority
// @Summary 创建角色
// @Security ApiKeyAuth
// @accept application/json
@ -34,7 +34,7 @@ func CreateAuthority(c *gin.Context) {
}
}
// @Tags authority
// @Tags Authority
// @Summary 拷贝角色
// @Security ApiKeyAuth
// @accept application/json
@ -61,7 +61,7 @@ func CopyAuthority(c *gin.Context) {
}
}
// @Tags authority
// @Tags Authority
// @Summary 删除角色
// @Security ApiKeyAuth
// @accept application/json
@ -84,7 +84,7 @@ func DeleteAuthority(c *gin.Context) {
}
}
// @Tags authority
// @Tags Authority
// @Summary 更新角色信息
// @Security ApiKeyAuth
// @accept application/json
@ -107,7 +107,7 @@ func UpdateAuthority(c *gin.Context) {
}
}
// @Tags authority
// @Tags Authority
// @Summary 分页获取角色列表
// @Security ApiKeyAuth
// @accept application/json
@ -135,7 +135,7 @@ func GetAuthorityList(c *gin.Context) {
}
}
// @Tags authority
// @Tags Authority
// @Summary 设置角色资源权限
// @Security ApiKeyAuth
// @accept application/json

649
server/docs/docs.go
File diff suppressed because it is too large
View File

646
server/docs/swagger.json
File diff suppressed because it is too large
View File

512
server/docs/swagger.yaml
File diff suppressed because it is too large
View File

4
server/middleware/casbin_rbac.go

@ -2,8 +2,8 @@ package middleware
import (
"gin-vue-admin/global"
"gin-vue-admin/global/response"
"gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"gin-vue-admin/service"
"github.com/gin-gonic/gin"
)
@ -25,7 +25,7 @@ func CasbinHandler() gin.HandlerFunc {
if global.GVA_CONFIG.System.Env == "develop" || success {
c.Next()
} else {
response.Result(response.ERROR, gin.H{}, "权限不足", c)
response.FailWithDetailed(gin.H{}, "权限不足", c)
c.Abort()
return
}

45
server/middleware/jwt.go

@ -3,9 +3,9 @@ package middleware
import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/global/response"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"gin-vue-admin/service"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
@ -19,16 +19,12 @@ func JWTAuth() gin.HandlerFunc {
// 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
token := c.Request.Header.Get("x-token")
if token == "" {
response.Result(response.ERROR, gin.H{
"reload": true,
}, "未登录或非法访问", c)
response.FailWithDetailed(gin.H{"reload": true}, "未登录或非法访问", c)
c.Abort()
return
}
if service.IsBlacklist(token) {
response.Result(response.ERROR, gin.H{
"reload": true,
}, "您的帐户异地登陆或令牌失效", c)
response.FailWithDetailed(gin.H{"reload": true}, "您的帐户异地登陆或令牌失效", c)
c.Abort()
return
}
@ -37,40 +33,33 @@ func JWTAuth() gin.HandlerFunc {
claims, err := j.ParseToken(token)
if err != nil {
if err == TokenExpired {
response.Result(response.ERROR, gin.H{
"reload": true,
}, "授权已过期", c)
response.FailWithDetailed(gin.H{"reload": true}, "授权已过期", c)
c.Abort()
return
}
response.Result(response.ERROR, gin.H{
"reload": true,
}, err.Error(), c)
response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
c.Abort()
return
}
if err, _ = service.FindUserByUuid(claims.UUID.String()); err != nil{
response.Result(response.ERROR, gin.H{
"reload": true,
}, err.Error(), c)
if err, _ = service.FindUserByUuid(claims.UUID.String()); err != nil {
response.FailWithDetailed(gin.H{"reload": true}, err.Error(), c)
c.Abort()
}
if claims.ExpiresAt - time.Now().Unix()<claims.BufferTime {
if claims.ExpiresAt-time.Now().Unix() < claims.BufferTime {
claims.ExpiresAt = time.Now().Unix() + 60*60*24*7
newToken,_ := j.CreateToken(*claims)
newClaims,_ := j.ParseToken(newToken)
c.Header("new-token",newToken)
c.Header("new-expires-at",strconv.FormatInt(newClaims.ExpiresAt,10))
newToken, _ := j.CreateToken(*claims)
newClaims, _ := j.ParseToken(newToken)
c.Header("new-token", newToken)
c.Header("new-expires-at", strconv.FormatInt(newClaims.ExpiresAt, 10))
if global.GVA_CONFIG.System.UseMultipoint {
err,RedisJwtToken := service.GetRedisJWT(newClaims.Username)
if err!=nil {
global.GVA_LOG.Error("get redis jwt failed", zap.Any("err", err))
}else{
err, RedisJwtToken := service.GetRedisJWT(newClaims.Username)
if err != nil {
global.GVA_LOG.Error("get redis jwt failed", zap.Any("err", err))
} else { // 当之前的取成功时才进行拉黑操作
_ = service.JsonInBlacklist(model.JwtBlacklist{Jwt: RedisJwtToken})
//当之前的取成功时才进行拉黑操作
}
// 无论如何都要记录当前的活跃状态
_ = service.SetRedisJWT(newToken,newClaims.Username)
_ = service.SetRedisJWT(newToken, newClaims.Username)
}
}
c.Set("claims", claims)

Loading…
Cancel
Save