beego: XSRF support Controller level fix #610
default value is true when you Enable Global XSRF, also can control in the prepare function to change the value.
Showing
3 changed files
with
14 additions
and
4 deletions
| ... | @@ -47,6 +47,7 @@ type Controller struct { | ... | @@ -47,6 +47,7 @@ type Controller struct { |
| 47 | XSRFExpire int | 47 | XSRFExpire int |
| 48 | AppController interface{} | 48 | AppController interface{} |
| 49 | EnableRender bool | 49 | EnableRender bool |
| 50 | EnableXSRF bool | ||
| 50 | } | 51 | } |
| 51 | 52 | ||
| 52 | // ControllerInterface is an interface to uniform all controller handler. | 53 | // ControllerInterface is an interface to uniform all controller handler. |
| ... | @@ -76,6 +77,7 @@ func (c *Controller) Init(ctx *context.Context, controllerName, actionName strin | ... | @@ -76,6 +77,7 @@ func (c *Controller) Init(ctx *context.Context, controllerName, actionName strin |
| 76 | c.TplExt = "tpl" | 77 | c.TplExt = "tpl" |
| 77 | c.AppController = app | 78 | c.AppController = app |
| 78 | c.EnableRender = true | 79 | c.EnableRender = true |
| 80 | c.EnableXSRF = true | ||
| 79 | c.Data = ctx.Input.Data | 81 | c.Data = ctx.Input.Data |
| 80 | } | 82 | } |
| 81 | 83 | ||
| ... | @@ -441,6 +443,9 @@ func (c *Controller) XsrfToken() string { | ... | @@ -441,6 +443,9 @@ func (c *Controller) XsrfToken() string { |
| 441 | // the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" | 443 | // the token can provided in request header "X-Xsrftoken" and "X-CsrfToken" |
| 442 | // or in form field value named as "_xsrf". | 444 | // or in form field value named as "_xsrf". |
| 443 | func (c *Controller) CheckXsrfCookie() bool { | 445 | func (c *Controller) CheckXsrfCookie() bool { |
| 446 | if !c.EnableXSRF { | ||
| 447 | return true | ||
| 448 | } | ||
| 444 | token := c.GetString("_xsrf") | 449 | token := c.GetString("_xsrf") |
| 445 | if token == "" { | 450 | if token == "" { |
| 446 | token = c.Ctx.Request.Header.Get("X-Xsrftoken") | 451 | token = c.Ctx.Request.Header.Get("X-Xsrftoken") | ... | ... |
| ... | @@ -906,6 +906,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -906,6 +906,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 906 | //call the controller init function | 906 | //call the controller init function |
| 907 | execController.Init(context, runrouter.Name(), runMethod, vc.Interface()) | 907 | execController.Init(context, runrouter.Name(), runMethod, vc.Interface()) |
| 908 | 908 | ||
| 909 | //call prepare function | ||
| 910 | execController.Prepare() | ||
| 911 | |||
| 909 | //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf | 912 | //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf |
| 910 | if EnableXSRF { | 913 | if EnableXSRF { |
| 911 | execController.XsrfToken() | 914 | execController.XsrfToken() |
| ... | @@ -915,9 +918,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) | ... | @@ -915,9 +918,6 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) |
| 915 | } | 918 | } |
| 916 | } | 919 | } |
| 917 | 920 | ||
| 918 | //call prepare function | ||
| 919 | execController.Prepare() | ||
| 920 | |||
| 921 | if !w.started { | 921 | if !w.started { |
| 922 | //exec main logic | 922 | //exec main logic |
| 923 | switch runMethod { | 923 | switch runMethod { | ... | ... |
| ... | @@ -186,16 +186,21 @@ func Htmlunquote(src string) string { | ... | @@ -186,16 +186,21 @@ func Htmlunquote(src string) string { |
| 186 | 186 | ||
| 187 | // UrlFor returns url string with another registered controller handler with params. | 187 | // UrlFor returns url string with another registered controller handler with params. |
| 188 | // usage: | 188 | // usage: |
| 189 | // | ||
| 189 | // UrlFor(".index") | 190 | // UrlFor(".index") |
| 190 | // print UrlFor("index") | 191 | // print UrlFor("index") |
| 192 | // router /login | ||
| 191 | // print UrlFor("login") | 193 | // print UrlFor("login") |
| 192 | // print UrlFor("login", "next","/"") | 194 | // print UrlFor("login", "next","/"") |
| 193 | // print UrlFor("profile", "username","John Doe") | 195 | // router /profile/:username |
| 196 | // print UrlFor("profile", ":username","John Doe") | ||
| 194 | // result: | 197 | // result: |
| 195 | // / | 198 | // / |
| 196 | // /login | 199 | // /login |
| 197 | // /login?next=/ | 200 | // /login?next=/ |
| 198 | // /user/John%20Doe | 201 | // /user/John%20Doe |
| 202 | // | ||
| 203 | // more detail http://beego.me/docs/mvc/controller/urlbuilding.md | ||
| 199 | func UrlFor(endpoint string, values ...string) string { | 204 | func UrlFor(endpoint string, values ...string) string { |
| 200 | return BeeApp.UrlFor(endpoint, values...) | 205 | return BeeApp.UrlFor(endpoint, values...) |
| 201 | } | 206 | } | ... | ... |
-
Please register or sign in to post a comment