This is a creation in Article, where the information may have evolved or changed.
Beego supports custom filtering middleware, such as security verification, forced jumps, and so on.
The filter functions are as follows:
Beego. Insertfilter (pattern string, postion int, filter filterfunc, params ... bool)
Three required parameters of the Insertfilter function, an optional parameter
· pattern routing rules that can be routed according to certain rules, if you have a full match *
· postion where the Filter is executed, the four fixed parameters are as follows, representing different execution processes
1. Beforerouter before looking for routes
2. beforeexec After locating the route, start executing the appropriate Controller
3. afterexec The filter executed after the Controller logic is executed
4. finishrouter Filter executed after execution of logic
· The filter filter function type Filterfunc func (*context. Context)
· params
1. Set the value of the Returnonoutput (default true), whether or not to allow the output to skip the other filters, by default no additional filters will be performed as long as there is output
2.Whether to reset the parameters of the filters, the default is false, because in the filters pattern and its own route pattern conflict, you can reset the parameters of the filters, which will ensure that the subsequent logic to obtain the correct parameters, such as set the /api/*filter, and also set the/api/docs/*the router, then in the Access/api/docs/swagger/abc.jsWhen the filters is executed, set the: Splatparameter isDocs/swagger/abc.js, but if you do not know the route parameter of the filter, it will be maintained when the routing logic is executed.Docs/swagger/abc.jsResets if True is set.: Splatparameters.
AddFilter has been abolished since the beego1.3 version
Verify that the user is logged in and apply to all requests as shown in the following example:
varFilteruser= func (ctx *context.Context) {_, OK: = ctx.Input.Session("UID"). (int) if!ok && ctx.Request.RequestUri != "/login"{ctx.Redirect(302, "/login")}} beego.Insertfilter("/*", Beego.Beforerouter,Filteruser)
It is important to note that the Filter using the session must be available after beforestatic because the session is not initialized before.
You can also filter by regular routing if the matching parameters are executed:
varFilteruser= func (ctx *context.Context) {_, OK: = ctx.Input.Session("UID"). (int) if!ok {ctx.Redirect(302, "/login")}}beego.Insertfilter("/user/:id ([0-9]+)", Beego.Beforerouter,Filteruser)
Filter Implementation Routing
beego1.1.2 starts with the addition of Runcontroller and Runmethod in the context.input, so that we can implement our own routing rules in filter before performing a route lookup.
The following example implements how to implement your own routing rules:
varUrlmanager= func (ctx *context.Context) { //Database reads all URLs mapping dataurlmapping: = model.geturlmapping() for Baseurl,rule:=range urlmapping {if BaseURL = = ctx.Request.RequestUri{ctx.Input.Runcontroller= Rule.controller ctx.Input.Runmethod= Rule.method Break}}} Beego.Insertfilter("/*", Beego.Beforerouter,Urlmanager)
Context. some common methods in the Context
// gets the request path in the Cookies value
Func (CTX *context) GetCookie (key string) string {
Return CTX. Input.cookie (Key)
}
set The value of a cookie
Func (CTX *context) Setcookie (name string, value string, others ... interface{}) {
CTx. Output.cookie (name, value, others ...)
}
Writes a string to the response body (write string to response body)
Func (CTX *context) writestring (content string) {
CTx. Responsewriter.write ([]byte (content))
}
redirect status as state code,localurl redirect Path
Func (CTX *context) Redirect (status int, localurl string) {
CTx. Output.header ("Location", Localurl)
CTx. Responsewriter.writeheader (status)
}
Disclaimer: Part of the content to beego.me official website