3

目前我在我的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 文档,因为 revel 已经经历了相当多的发展

4

1 回答 1

2

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

初始化.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 实现供您参考。

于 2014-11-28T18:03:33.557 回答