-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
63 lines (39 loc) · 1.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main
import (
"github.com/kataras/iris/v12"
"github.com/casbin/casbin/v2"
cm "casebin-middleware/casbin"
redisadapter "github.com/casbin/redis-adapter/v2"
)
func newApp() *iris.Application {
//配置casbin权限数据同步到REDIS
adp := redisadapter.NewAdapter("tcp", "127.0.0.1:6379")
var Enforcer, _ = casbin.NewEnforcer("casbinmodel.conf", adp)
//设置初始权限 默认ADMIN用户拥有所有权限
Enforcer.LoadPolicy()
Enforcer.AddPolicy("admin", "/*", "*")
Enforcer.AddPolicy("anonymous", "/", "GET")
Enforcer.AddPolicy("member", "/logout", "*")
Enforcer.AddPolicy("member", "/member/*", "*")
if err := Enforcer.SavePolicy(); err != nil {
panic(err)
}
//设置权限中间件
casbinMiddleware := cm.New(Enforcer)
app := iris.New()
app.Use(casbinMiddleware.ServeHTTP)//调用权限中间件
app.Get("/", hi)
app.Get("/dataset1/{p:path}", hi) // p, alice, /dataset1/*, GET
app.Post("/dataset1/resource1", hi)
app.Get("/dataset2/resource2", hi)
app.Post("/dataset2/folder1/{p:path}", hi)
app.Any("/dataset2/resource1", hi)
return app
}
func main() {
app := newApp()
app.Run(iris.Addr(":8081"))
}
func hi(ctx iris.Context) {
ctx.Writef("Hello %s", cm.Username(ctx.Request()))
}