http.authz

Caddy-authz是Caddy的授权中间件,它基于Caddy-authz:https://github.com/casbin/casbin

Casbin是一个基于Golang实现的功能强大、高效的开源访问控制库。它提供了基于ACL、RBAC、ABAC等各种模型的强制授权支持。

完整文档

示例

简单示例

  1. package main
  2. import (
  3. "net/http"
  4. "github.com/casbin/caddy-authz"
  5. "github.com/casbin/casbin"
  6. "github.com/mholt/caddy/caddyhttp/httpserver"
  7. )
  8. func main() {
  9. // load the casbin model and policy from files, database is also supported.
  10. e := casbin.NewEnforcer("authz_model.conf", "authz_policy.csv")
  11. // define your handler, this is just an example to return HTTP 200 for any requests.
  12. // the access that is denied by authz will return HTTP 403 error.
  13. handler := authz.Authorizer{
  14. Next: httpserver.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
  15. return http.StatusOK, nil
  16. }),
  17. Enforcer: e,
  18. }
  19. }

使用简单的模型文件和策略文件对HTTP请求进行授权。