Ai
1 Star 1 Fork 0

李沐风岚/murphy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
auth.go 1.50 KB
一键复制 编辑 原始数据 按行查看 历史
Your Name 提交于 2025-07-31 10:04 +08:00 . up
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/oshine/murphy.git
git@gitee.com:oshine/murphy.git
oshine
murphy
murphy
v1.0.26

搜索帮助