我正在构建一个身份验证系统,到目前为止,我对它的工作非常满意。但是现在我想要集成一个通配符运算符,如下所示:
如果uri是/ user / list并且在允许的地图中有/ user / *它必须通过。
Allowed {
"*": {"administrator", "regional"}, // logic works
"/user/*": {"administrator"}, // how to implement
"/login": {"administrator", "regional"}, // logic works
}
func (a *Authentication) IsAllowed(req *http.Request, role string) error {
schema := a.Schema // = the Allowed map[string][]string above
url := req.URL.String()
// Check strict match of the url in the schema
roles, ok := schema[url]
if ok {
if util.InSlice(role, roles) {
return nil
} else {
return USERNOTALLOWED // error
}
}
// here must come the logic of the wildcardsuffix
if a.hasWildCardSuffix(url string) {
}
// Fallback to wildCard *
if a.hasWildCard() { // return a bool whenever there is a "*" key
roles, _ = a.Schema["*"]
if util.InSlice(role, roles) {
return nil
} else {
return USERNOTALLOWED // error
}
}
return nil
}
thx很多
答案 0 :(得分:1)
文件路径有Match功能可以为您执行此操作:
package main
import (
"log"
"path/filepath"
)
func main() {
ok, err := filepath.Match("/user/*", "/user/list")
log.Print(err)
log.Print(ok)
ok, err = filepath.Match("/user/*/*", "/user/list/detail")
log.Print(err)
log.Print(ok)
}
playground:http://play.golang.org/p/DZ2yVmi5zs