美文网首页
beego 验证登录路由过滤器

beego 验证登录路由过滤器

作者: charmingcheng | 来源:发表于2021-08-09 14:25 被阅读0次

    filter.go

    package utils
    
    import (
        "github.com/astaxie/beego"
        "github.com/astaxie/beego/context"
    )
    // 不需要验证登陆的路由
    var NOTFILTERROUTER = []string{
        "/admin/login",
    }
    
    type Filter struct {
        Response
    }
    
    func (f *Filter) FilterLoginStatus() beego.FilterFunc {
        filter := func(ctx *context.Context) {
            f.Ctx = ctx
            ok := ctx.Input.Session("user")
            uri := ctx.Request.URL.Path
            exist := IsContainInList(NOTFILTERROUTER, uri)
    
            if ok == nil && !exist {
                f.Error("WithoutLogin")
                f.Ctx.Redirect(302, "/admin/login")
            }
        }
        return filter
    }
    
    func IsContainInList(items []string, item string) bool {
        for _, v := range items {
            if item == v {
                return true
            }
        }
        return false
    }
    

    response.go

    package utils
    
    import "github.com/astaxie/beego"
    
    type Response struct {
        beego.Controller
    }
    
    type any = interface {}
    type Err map[string]Json
    
    type Json struct {
        Code int            `json:"code"`
        Msg string          `json:"msg"`
        Data interface{}    `json:"data"`
    }
    
    var AllCODE_MSG = Err {
        "UnknownErr":           Json{Code:10000, Msg: "未知错误"},
    
        "InternalServerError":  Json{Code:10001, Msg: "内部服务器错误"},
        "ErrBind":              Json{Code:10002, Msg: "请求参数错误"},
        "ErrTokenSign":         Json{Code:10003, Msg: "签名jwt时发生错误"},
        "ErrEncrypt":           Json{Code:10004, Msg: "加密用户密码时发生错误"},
    
        // 数据库错误, 前缀为 201
        "ErrDatabase":          Json{Code:20100, Msg: "数据库错误"},
        "ErrFill":              Json{Code:20101, Msg: "数据库填充 struct 时发生错误"},
        "HandleDbErr":          Json{Code:20102, Msg: "数据库执行错误"},
    
        // 认证错误, 前缀是 202
        "ErrValidation":        Json{Code:20201, Msg: "验证失败"},
        "ErrTokenInvalid":      Json{Code:20202, Msg: "无效的token"},
        "WithoutLogin":         Json{Code:20203, Msg: "账号未登录"},
    
        // 用户错误, 前缀为 203
        "ErrUserNotFound":      Json{Code: 20301, Msg: "账号不存在"},
        "ErrPasswordIncorrect": Json{Code: 20302, Msg: "密码错误"},
    }
    
    func ErrMsg(errName string) Json {
        if v, ok := AllCODE_MSG[errName]; ok {
            return v
        } else {
            return Json{Code:1, Msg: errName}
        }
    }
    
    func (r *Response) Success(msg string, data any) Json {
    
        result := Json{Code:0, Msg: msg, Data: data}
    
        return result
    }
    
    func (r *Response) Error(msg string) Json {
        result := ErrMsg(msg)
        return result
    }
    

    使用

    在main.go中使用

    package main
    
    import (
        _ "beego-admin/routers"
        "beego-admin/utils"
        "github.com/astaxie/beego"
    )
    
    func main() {
    
        // 路由过滤
        filter := &utils.Filter{}
        beego.InsertFilter("/admin/*", beego.BeforeRouter, filter.FilterLoginStatus())
    
        beego.Run()
    }
    

    相关文章

      网友评论

          本文标题:beego 验证登录路由过滤器

          本文链接:https://www.haomeiwen.com/subject/caezvltx.html