Update Readme.md

This commit is contained in:
weloe
2023-05-13 15:15:55 +08:00
parent ab16d567f8
commit 7905f81270
2 changed files with 122 additions and 0 deletions

View File

@@ -9,11 +9,27 @@ import (
var enforcer *tokenGo.Enforcer
type Auth struct {
}
func (m *Auth) GetRole(id string) []string {
var arr = make([]string, 2)
arr[1] = "user"
return arr
}
func (m *Auth) GetPermission(id string) []string {
var arr = make([]string, 2)
arr[1] = "user::get"
return arr
}
func main() {
var err error
// use default adapter
adapter := tokenGo.NewDefaultAdapter()
enforcer, err = tokenGo.NewEnforcer(adapter)
// set auth
enforcer.SetAuth(&Auth{})
// enable logger
enforcer.EnableLog()
if err != nil {
@@ -24,10 +40,26 @@ func main() {
http.HandleFunc("/user/logout", Logout)
http.HandleFunc("/user/isLogin", IsLogin)
http.HandleFunc("/user/kickout", Kickout)
http.HandleFunc("/user/check", CheckAuth)
log.Fatal(http.ListenAndServe(":8081", nil))
}
func CheckAuth(w http.ResponseWriter, req *http.Request) {
ctx := tokenGo.NewHttpContext(req, w)
err := enforcer.CheckRole(ctx, "user")
if err != nil {
fmt.Fprintf(w, "CheckRole() error: %s\n", err)
return
}
err = enforcer.CheckPermission(ctx, "user::get")
if err != nil {
fmt.Fprintf(w, "CheckPermission() error: %s\n", err)
return
}
fmt.Fprintf(w, "you have authorization")
}
func Login(w http.ResponseWriter, req *http.Request) {
token, err := enforcer.Login("1", tokenGo.NewHttpContext(req, w))
if err != nil {