Play Framework使用模板引擎进行身份验证时隐藏链接

时间:2013-11-09 14:42:23

标签: java templates playframework

我正在使用播放框架和它的模板引擎做一个网站。一切都很好,除非我对登录时链接仍然可见这一事实感到不满。

   <div class="topbar">
    <div class="fill">
        <div class="container">
            <a class="brand" href="@routes.Application.index()">L8</a>
            <ul class="nav">
                <li><a href="@routes.RegisterForm.register()">Register</a></li>
                <li><a href="@routes.Terms.terms()">ToS</a></li>
            </ul>
        </div>
    </div>
</div>

在通过身份验证时是否有任何方法可以隐藏导航栏中的链接

2 个答案:

答案 0 :(得分:0)

我怀疑你想自己实施所有授权。我建议用户玩Deadbolt模块:https://github.com/schaloner/deadbolt-2。它具有所有必需的功能。

答案 1 :(得分:0)

具体的解决方案当然取决于您的特定身份验证解决方案。但基本上你可以使用辅助类/对象中的一些函数来告诉你用户是否登录,并且根据这些信息你可以显示注册链接。

对于一个游戏演示,我这样做了,你可以在this template checking if the user is logged in中看到:

@ShopsController.loggedInUser.map { username =>
  Logged in user: @username
}.getOrElse {
  <a href="...">Login</a>
}

ShopsController(可能是任何其他帮助对象)分别是我ShopsSecurity trait checks if the user is logged in and if so retrieves the username play2-java-computer-database sample main template

ShopsController

此检查显然取决于基于Play内置def loggedInUser(implicit request: Request[_]): Option[String] = request match { case authReq: Security.AuthenticatedRequest[_, _] => Some(authReq.user.asInstanceOf[String]) case _ => None }

的实现

如果您正在寻找使用Java的示例,可以查看我的Secured helper class,特别是{{3}}和{{3}}。