You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
632 B

  1. package middleware
  2. import (
  3. "gin-vue-admin/global/response"
  4. "gin-vue-admin/model"
  5. "github.com/gin-gonic/gin"
  6. )
  7. //拦截器
  8. func CasbinHandler() gin.HandlerFunc {
  9. return func(c *gin.Context) {
  10. claims, _ := c.Get("claims")
  11. waitUse := claims.(*CustomClaims)
  12. //获取请求的URI
  13. obj := c.Request.URL.RequestURI()
  14. //获取请求方法
  15. act := c.Request.Method
  16. //获取用户的角色
  17. sub := waitUse.AuthorityId
  18. e := model.Casbin()
  19. //判断策略中是否存在
  20. if e.Enforce(sub, obj, act) {
  21. c.Next()
  22. } else {
  23. response.Result(response.ERROR, gin.H{}, "权限不足", c)
  24. c.Abort()
  25. return
  26. }
  27. }
  28. }