简体   繁体   English

如何使用安全模块从视图检查用户是否已连接

[英]How can I check if a user is connected from a view, using the Secure module

I'm using the Secure module if that wasn't clear from my title: http://www.playframework.org/documentation/1.2.4/secure 如果标题不清楚,我正在使用安全模块: http : //www.playframework.org/documentation/1.2.4/secure

(The Security controller resides in the (standard) controllers package) (安全控制器位于(标准)控制器软件包中)

I have this in my main.html template: 我的main.html模板中有这个:

#{if Security.isConnected != true}
    <a href="#" id="loginSlider">Login</a>
#{/if}
#{else}
    <a href="/logout">Logout</a>
#{/else}

And it doesn't seem to work as "Security" is null from here. 它似乎不起作用,因为“ Security”从此处为空。 I've found it to be fairly poorly documented on playframework.org. 我发现它在playframework.org上的记录相当少。

[EDIT] Things I've tried: Adding a @ in front of the Controller: #{if @Security.isConnected != true} Prefixing with package name: #{if @controllers.Security.isConnected != true} [编辑]我尝试过的事情:在Controller前面添加一个@:#{if @ Security.isConnected!= true}带包名称的前缀:#{if @ controllers.Security.isConnected!= true}

I've verified that I am indeed logged in by navigating to a secure page. 我已通过导航到安全页面来验证自己确实已登录。

[EDIT2] Found a workaround that works, but I dislike it. [EDIT2]找到了一种可行的解决方法,但我不喜欢它。 Basically, the Security controller method isConnected does this: return session.contains("username"). 基本上,安全控制器方法isConnected会执行此操作:返回session.contains(“ username”)。 So I can do that check in my view: 因此,我可以在我的视图中进行检查:

#{ifnot session.contains("username")}

But its not the optimal solution... 但这不是最佳解决方案...

#{ifnot controllers.Security.isConnected()}
    <a href="#" id="loginSlider">Login</a>
#{/ifnot}
#{else}
    <a href="/logout">Logout</a>
#{/else}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM