doufan8805 2014-11-04 12:00 采纳率: 0%
浏览 10
已采纳

更好的(一般)身份验证实现

Currently I have a method in my BaseController and in each controller method that I need the user to be authenticated I am left with always calling this piece of code:

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

Which just checks if

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

(in the init.go) has added a valid user to .RenderArgs["user"].

Is there anyway I can put this redirect to the login page incl. the auth check into an filter / intercept method, so I don't have to repeat the above code 10 times? (I developed this code around revel v0.9~0.10)

One solution I came up with would be writting a module/app similiar to the new csrf module.

EDIT 4.11.2015: This Question was posted sometime ago, please check back the official Revel documentation as revel has undergone quite some development

  • 写回答

1条回答 默认 最新

  • duanmen1887 2014-11-28 18:03
    关注

    Just don't let the requests to your controllers unless if authentication has properly been done. You need to implement a Filter for that. It means something like

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

    The specifics depend entirely on what kind of authentication you are setting up. Here is one SSO implementation for your reference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器