Go 更好的(通用)身份验证实现

Go 更好的(通用)身份验证实现,go,revel,Go,Revel,目前,我在我的BaseController中有一个方法,在每个需要验证用户身份的控制器方法中,我总是调用这段代码: user, err := c.getUser() if err != nil { return c.Redirect(UserController.Login) } 这只是检查一下 revel.InterceptMethod((*UserController).CheckUser, revel.BEFORE) (在init.go中) 已将有效用户添加到.Rend

目前,我在我的
BaseController
中有一个方法,在每个需要验证用户身份的控制器方法中,我总是调用这段代码:

user, err := c.getUser()
if err != nil {
        return c.Redirect(UserController.Login)
}
这只是检查一下

revel.InterceptMethod((*UserController).CheckUser, revel.BEFORE)
(在init.go中) 已将有效用户添加到
.RenderArgs[“user”]

我是否可以将此重定向到登录页面(包括身份验证检查)放入筛选/拦截方法中,这样我就不必重复上述代码10次? (我围绕revel v0.9~0.10开发了此代码)

我提出的一个解决方案是编写一个与新的csrf模块类似的模块/应用程序


编辑4.11.2015:这个问题是前一段时间发布的,请查看官方,因为revel已经经历了相当多的发展

除非正确完成了身份验证,否则不要将请求发送给您的控制器。您需要为此实现一个新的解决方案。它的意思是

init.go:

revel.Filters = []revel.Filter{
    SessionFilter, // Preferably a safe implementation that isn't plaintext cookies etc
    mypackage.Authenticator
}
mypackage.go:

package mypackage

func Authenticator(c *revel.Controller, fc []revel.Filter) {
 // If authentication found (from session), pass to next Filter in stack
 // If not, redirect to your authentication UI, and pass
 // Or handle other parts of authentication requests...
 // If authentication succeeded, save it to session

 // Otherwise just drop the request (probably log?)
}
具体细节完全取决于您设置的身份验证类型。这里有一个供你参考