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

时间:2014-11-04 12:00:58

标签: go revel

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

user, err := c.getUser()
if err != nil {
        return c.Redirect(UserController.Login)
}

只检查是否

revel.InterceptMethod((*UserController).CheckUser, revel.BEFORE)

(在init.go中) 已将有效用户添加到.RenderArgs["user"]

无论如何,我可以将此重定向添加到登录页面中。 auth检查过滤器/拦截方法,所以我不必重复上述代码10次? (我在revel v0.9~0.10之间开发了这段代码)

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

编辑4.11.2015:此问题是在不久前发布的,请回复官方Revel documentation,因为狂欢已经经历了一些发展

1 个答案:

答案 0 :(得分:2)

除非正确完成身份验证,否则不要将请求发送给您的控制器。您需要为此实现Filter。它意味着类似

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?)
}

具体细节完全取决于您设置的身份验证类型。这是一个SSO implementation供您参考。

相关问题