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. 43
      server/middleware/jwt.go

12
server/api/v1/sys_authority.go

@ -11,7 +11,7 @@ import (
"go.uber.org/zap" "go.uber.org/zap"
) )
// @Tags authority
// @Tags Authority
// @Summary 创建角色 // @Summary 创建角色
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
@ -34,7 +34,7 @@ func CreateAuthority(c *gin.Context) {
} }
} }
// @Tags authority
// @Tags Authority
// @Summary 拷贝角色 // @Summary 拷贝角色
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
@ -61,7 +61,7 @@ func CopyAuthority(c *gin.Context) {
} }
} }
// @Tags authority
// @Tags Authority
// @Summary 删除角色 // @Summary 删除角色
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
@ -84,7 +84,7 @@ func DeleteAuthority(c *gin.Context) {
} }
} }
// @Tags authority
// @Tags Authority
// @Summary 更新角色信息 // @Summary 更新角色信息
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
@ -107,7 +107,7 @@ func UpdateAuthority(c *gin.Context) {
} }
} }
// @Tags authority
// @Tags Authority
// @Summary 分页获取角色列表 // @Summary 分页获取角色列表
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
@ -135,7 +135,7 @@ func GetAuthorityList(c *gin.Context) {
} }
} }
// @Tags authority
// @Tags Authority
// @Summary 设置角色资源权限 // @Summary 设置角色资源权限
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @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 ( import (
"gin-vue-admin/global" "gin-vue-admin/global"
"gin-vue-admin/global/response"
"gin-vue-admin/model/request" "gin-vue-admin/model/request"
"gin-vue-admin/model/response"
"gin-vue-admin/service" "gin-vue-admin/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -25,7 +25,7 @@ func CasbinHandler() gin.HandlerFunc {
if global.GVA_CONFIG.System.Env == "develop" || success { if global.GVA_CONFIG.System.Env == "develop" || success {
c.Next() c.Next()
} else { } else {
response.Result(response.ERROR, gin.H{}, "权限不足", c)
response.FailWithDetailed(gin.H{}, "权限不足", c)
c.Abort() c.Abort()
return return
} }

43
server/middleware/jwt.go

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

Loading…
Cancel
Save