代码拉取完成,页面将自动刷新
package ctl
import (
"log"
"os"
"github.com/goccy/go-json"
"github.com/valyala/fasthttp"
)
func (m *AuthController) SetSkipRule(r *[][]string) {
m._skipRule = r
}
// 读取跳过路由
func ReadSkipRouter(r *[][]string,configPath ...string){
var path string
if len(configPath)>0 {
path=configPath[0]
}else{
path="skip_router.json"
}
bytes, err := os.ReadFile(path)
if err != nil {
return
}
err=json.Unmarshal(bytes, r)
if err!=nil {
log.Println(err.Error())
return
}
}
type AuthController struct {
Ctx
checkAuth func(ctx *fasthttp.RequestCtx) bool
beforeReq func(ctx *fasthttp.RequestCtx) bool
_skipRule *[][]string
}
func (m *AuthController) SetBeforeReq(req func(ctx *fasthttp.RequestCtx) bool) {
if m.beforeReq != nil {
return
}
m.beforeReq=req
}
func (m *AuthController) SetCheckAuth(auth func( ctx *fasthttp.RequestCtx) bool) {
if m.checkAuth != nil {
return
}
m.checkAuth=auth
}
// Auth 所有控制层的鉴权方法
// 返回 true 表示成功
func (m *AuthController) Auth(ctx *fasthttp.RequestCtx, name, action string) bool {
if !m.beforeReq(ctx) {
return false
}
if m._skipRule != nil {
for _, i2 := range *m._skipRule {
if i2[0] == name {
for j := 1; j < len(i2); j++ {
if action == i2[j] {
return true
}
}
break
}
}
}
if m.checkAuth(ctx) {
return true
}
b, _ := json.Marshal(&Msg{
Code: -1,
Msg: "auth fail",
})
ctx.Response.Header.Set("Content-Type", "application/json")
ctx.Response.SetBody(b)
return false
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。