String 如何将通配符运算符*集成到数学验证路由
我正在go中建立一个认证系统,到目前为止,我对它的工作非常满意。但是现在我想集成一个通配符操作符,如下所示: 如果uri是/user/list,并且在允许的映射中有/user/*它必须是pasString 如何将通配符运算符*集成到数学验证路由,string,go,String,Go,我正在go中建立一个认证系统,到目前为止,我对它的工作非常满意。但是现在我想集成一个通配符操作符,如下所示: 如果uri是/user/list,并且在允许的映射中有/user/*它必须是pas Allowed { "*": {"administrator", "regional"}, // logic works "/user/*": {"administrator"}, // how to implement "/login": {"administrator", "regiona
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 alot文件路径有一个功能,可以为您执行以下操作:
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)
}
游乐场:在较长的路线上失败,如/user/list/create@user3396016使用此:/user/*/*