一,安装casbin
1,casbin项目代码的地址
https://github.com/casbin/casbin
2,为go安装casbin
liuhongdi@ku:~$ go get -u github.com/casbin/casbin/v2
说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017
说明:作者:刘宏缔 邮箱: [email protected]
二,演示项目的相关信息
1,项目地址
https://github.com/liuhongdi/digv11
2,项目功能说明:
/admin/* :只有role为 superAdmin时可访问
/article/* : role为superAdmin/user时均可访问
/home/*: role为任何用户时均可访问
3,项目结构:如图:
三,配置文件说明:
1,config/rbac_model.conf
-
-
-
-
-
-
-
-
-
-
-
e = some(where (p.eft == allow))
-
-
-
-
-
m = (r.sub == p.sub || p.sub == "*") && keyMatch(r.obj,p.obj) && (r.act == p.act || p.act == "*")
2,rbac2.csv
-
p,superAdmin,/article/*,*
-
-
-
说明:csv文件必须用相应的软件生成,如图:
四,go代码说明
1,homeController.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv11/pkg/result"
-
-
-
type HomeController struct{}
-
-
func NewHomeController() HomeController {
-
-
-
-
func (a *HomeController) Home(c *gin.Context) {
-
resultRes := result.NewResult(c)
-
resultRes.Success("this is home page");
-
-
2,adminController.go
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv11/pkg/result"
-
-
-
type HomeController struct{}
-
-
func NewHomeController() HomeController {
-
-
-
-
func (a *HomeController) Home(c *gin.Context) {
-
resultRes := result.NewResult(c)
-
resultRes.Success("this is home page");
-
-
3,ArticleController.go
-
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv11/pkg/page"
-
"github.com/liuhongdi/digv11/pkg/result"
-
"github.com/liuhongdi/digv11/pkg/validCheck"
-
"github.com/liuhongdi/digv11/request"
-
"github.com/liuhongdi/digv11/service"
-
-
-
type ArticleController struct{}
-
-
func NewArticleController() ArticleController {
-
return ArticleController{}
-
-
-
func (a *ArticleController) GetOne(c *gin.Context) {
-
result := result.NewResult(c)
-
param := request.ArticleRequest{ID: validCheck.StrTo(c.Param("id")).MustUInt64()}
-
valid, errs := validCheck.BindAndValid(c, ¶m)
-
-
result.Error(400,errs.Error())
-
-
-
-
-
-
-
-
-
-
articleOne,err := service.GetOneArticle(param.ID);
-
-
result.Error(404,"数据查询错误")
-
-
result.Success(&articleOne);
-
-
-
-
4,global/casbin.go
-
-
-
-
-
"github.com/casbin/casbin/v2"
-
-
-
-
-
-
Enforcer *casbin.Enforcer
-
-
-
-
func SetupCasbinEnforcer() (error) {
-
-
modelPath := dir + "/config/rbac_model.conf"
-
csvPath := dir + "/config/rbac2.csv"
-
fmt.Println("modelPath:"+modelPath);
-
fmt.Println("csvPath:"+csvPath);
-
-
Enforcer, errC = casbin.NewEnforcer(modelPath, csvPath)
-
-
-
-
log.Fatalf("SetupCasbinEnforcer err: %v", errC)
-
-
-
-
-
-
5,main.go
-
-
-
-
"github.com/gin-gonic/gin"
-
_ "github.com/jinzhu/gorm/dialects/mysql"
-
"github.com/liuhongdi/digv11/global"
-
"github.com/liuhongdi/digv11/router"
-
-
-
-
-
-
-
err := global.SetupSetting()
-
-
log.Fatalf("init.setupSetting err: %v", err)
-
-
-
-
err = global.SetupLogger()
-
-
log.Fatalf("init.SetupLogger err: %v", err)
-
-
-
-
err = global.SetupAccessLogger()
-
-
log.Fatalf("init.SetupAccessLogger err: %v", err)
-
-
-
-
err = global.SetupCasbinEnforcer()
-
-
log.Fatalf("init.SetupCasbinEnforcer err: %v", err)
-
global.Logger.Fatalf("init.SetupCasbinEnforcer err: %v", err)
-
-
-
-
err = global.SetupDBLink()
-
-
log.Fatalf("init.SetupLogger err: %v", err)
-
global.Logger.Fatalf("init.setupDBEngine err: %v", err)
-
-
-
global.Logger.Infof("------应用init结束")
-
-
-
-
-
-
-
global.Logger.Infof("------应用main函数开始")
-
-
gin.SetMode(global.ServerSetting.RunMode)
-
-
-
-
r.Run(":"+global.ServerSetting.HttpPort)
-
6,middleware/permission.go
-
-
-
-
-
"github.com/gin-gonic/gin"
-
"github.com/liuhongdi/digv11/global"
-
"github.com/liuhongdi/digv11/pkg/result"
-
-
-
func PermissionMiddleWare() gin.HandlerFunc {
-
return func(c *gin.Context) {
-
-
-
-
-
-
-
-
-
-
-
fmt.Println("role:"+role)
-
-
-
-
-
isPass, err := global.Enforcer.Enforce(role, p, m)
-
-
resultRes := result.NewResult(c)
-
resultRes.Error(2005,err.Error())
-
-
-
-
-
-
resultRes := result.NewResult(c)
-
resultRes.Error(2006,"无访问权限")
-
-
-
-
7,其他相关代码可访问github
五,测试效果
1,middleware/permission.go中,
role:="guest"
访问:
http://127.0.0.1:8000/home/home
返回:
访问:
http://127.0.0.1:8000/article/getone/2
返回:
访问:
http://127.0.0.1:8000/admin/admin
返回:
2,middleware/permission.go中,
role:="user"
访问:
http://127.0.0.1:8000/home/home
返回:
访问:
http://127.0.0.1:8000/article/getone/2
返回:
访问:
http://127.0.0.1:8000/admin/admin
返回:
3,middleware/permission.go中,
role:="superAdmin"
访问:
http://127.0.0.1:8000/home/home
返回:
访问:
http://127.0.0.1:8000/article/getone/2
返回:
访问:
http://127.0.0.1:8000/admin/admin
返回:
六,查看库的版本:
-
module github.com/liuhongdi/digv11
-
-
-
-
-
github.com/gin-gonic/gin v1.6.3
-
github.com/go-playground/universal-translator v0.17.0
-
github.com/go-playground/validator/v10 v10.2.0
-
github.com/jinzhu/gorm v1.9.16
-
github.com/lestrrat/go-file-rotatelogs v0.0.0-20180223000712-d3151e2a480f
-
github.com/lestrrat/go-strftime v0.0.0-20180220042222-ba3bf9c1d042 // indirect
-
github.com/magiconair/properties v1.8.4 // indirect
-
github.com/mitchellh/mapstructure v1.3.3 // indirect
-
github.com/pelletier/go-toml v1.8.1 // indirect
-
github.com/pkg/errors v0.9.1 // indirect
-
github.com/spf13/afero v1.4.1 // indirect
-
github.com/spf13/cast v1.3.1 // indirect
-
github.com/spf13/jwalterweatherman v1.1.0 // indirect
-
github.com/spf13/pflag v1.0.5 // indirect
-
github.com/spf13/viper v1.7.1
-
github.com/casbin/casbin/v2 v2.17.0
-
go.uber.org/multierr v1.6.0 // indirect
-
-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
-
golang.org/x/text v0.3.4 // indirect
-
gopkg.in/ini.v1 v1.62.0 // indirect
-
gopkg.in/yaml.v2 v2.3.0 // indirect
-
|
请发表评论