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.

31 lines
663 B

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