路由器中的过滤模式
Filter patterns in router
对于每个用户,Beego 应用程序在 /static/users/
下创建一个目录,格式为:/static/users/USER_ID/private
和 /static/users/USER_ID/public
,其中 USER_ID ID每个用户。
我想保护私人文件,以便只有拥有这些文件的用户才能使用过滤器进行访问。
路由器中的模式如下:
beego.InsertFilter("/static/users/:userId([0-9]+)/private/*", beego.BeforeRouter, controllers.ProtectPrivateUploads)
过滤函数如下:
var ProtectPrivateUploads = func(ctx *context.Context) {
fmt.Println("Protecting content")
}
相关的 URL 具有以下形式:
domain.com/static/users/USERID/private/123135645.png
问题是过滤器函数根本没有被调用,所以我假设我一定是在路由器的模式上做错了什么。
欢迎任何想法。
过滤器 beego.BeforeStatic
似乎还有另一个插入点,但未记录在 http://beego.me/docs/mvc/controller/filter.md
通过查看 https://github.com/astaxie/beego/blob/master/router.go 中的代码,这些是可以触发过滤器的接受位置:
const (
// default filter execution points
BeforeStatic = iota
BeforeRouter
BeforeExec
AfterExec
FinishRouter
)
因此触发静态文件过滤器的有效调用可能是:
beego.InsertFilter("/static/users/:userId([0-9]+)/private/*", beego.BeforeStatic, controllers.ProtectPrivateUploads)
更新
可以使用以下函数获取 beego.BeforeRouter
路由器位置的会话对象:
sess,_ := beego.GlobalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
因此,保护 /static/
url 下内容的有效路由器和过滤器将是:
路由器:
beego.InsertFilter("/static/users/:id([0-9]+)/private/*", beego.BeforeStatic, controllers.ProtectPrivateUploads)
过滤器:
var ProtectPrivateUploads = func(ctx *context.Context) {
sess,_ := beego.GlobalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
defer sess.SessionRelease(ctx.ResponseWriter)
ses := sess.Get("sessionid")
if ses != nil {
// get user's id from the session and check if the user can access the requested URL
}
对于每个用户,Beego 应用程序在 /static/users/
下创建一个目录,格式为:/static/users/USER_ID/private
和 /static/users/USER_ID/public
,其中 USER_ID ID每个用户。
我想保护私人文件,以便只有拥有这些文件的用户才能使用过滤器进行访问。
路由器中的模式如下:
beego.InsertFilter("/static/users/:userId([0-9]+)/private/*", beego.BeforeRouter, controllers.ProtectPrivateUploads)
过滤函数如下:
var ProtectPrivateUploads = func(ctx *context.Context) {
fmt.Println("Protecting content")
}
相关的 URL 具有以下形式:
domain.com/static/users/USERID/private/123135645.png
问题是过滤器函数根本没有被调用,所以我假设我一定是在路由器的模式上做错了什么。
欢迎任何想法。
过滤器 beego.BeforeStatic
似乎还有另一个插入点,但未记录在 http://beego.me/docs/mvc/controller/filter.md
通过查看 https://github.com/astaxie/beego/blob/master/router.go 中的代码,这些是可以触发过滤器的接受位置:
const (
// default filter execution points
BeforeStatic = iota
BeforeRouter
BeforeExec
AfterExec
FinishRouter
)
因此触发静态文件过滤器的有效调用可能是:
beego.InsertFilter("/static/users/:userId([0-9]+)/private/*", beego.BeforeStatic, controllers.ProtectPrivateUploads)
更新
可以使用以下函数获取 beego.BeforeRouter
路由器位置的会话对象:
sess,_ := beego.GlobalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
因此,保护 /static/
url 下内容的有效路由器和过滤器将是:
路由器:
beego.InsertFilter("/static/users/:id([0-9]+)/private/*", beego.BeforeStatic, controllers.ProtectPrivateUploads)
过滤器:
var ProtectPrivateUploads = func(ctx *context.Context) {
sess,_ := beego.GlobalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
defer sess.SessionRelease(ctx.ResponseWriter)
ses := sess.Get("sessionid")
if ses != nil {
// get user's id from the session and check if the user can access the requested URL
}