2 Xsrf
Lunny Xiao edited this page 2019-11-27 09:08:08 +08:00

Middleware xsrf is a xsrf checker for Tango.

Installation

go get gitea.com/tango/xsrf

Simple Example

type XsrfAction struct {
    render.Render
    xsrf.Checker
}

func (x *XsrfAction) Get() error {
    return x.Render("test.html", render.T{
        "XsrfFormHtml": x.XsrfFormHtml(),
    })
}

func (x *XsrfAction) Post() {
    // xsrf will be checked before this being called
}

func main() {
    t := tango.Classic()
    t.Use(xsrf.New(expireTime))
    t.Run()
}

If you don't want some action do not check, then

type NoCheckAction struct {
    xsrf.NoCheck
}

func (x *NoCheckAction) Post() {
    // xsrf will NOT be checked before this being called
}

will be ok.