如何检查用户是否登录? [英] How to check if is user logged in?

查看:193
本文介绍了如何检查用户是否登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在用户未登录时显示登录链接,并在用户登录时显示注销链接.我正在使用web.xml中定义的容器管理的安全性.

I want do display a login link when the user isn't logged in and a logout link when the user is logged in. I'm using container managed security as defined in web.xml.

我该如何实现?

推荐答案

The username of the logged-in user is available by ExternalContext#getRemoteUser() which delegates under the covers to HttpServletRequest#getRemoteUser(). Both are available in EL by #{facesContext.externalContext.remoteUser} and #{request.remoteUser} respectively. If it is null, then it means that the user is not logged in.

因此,在您看来,您可以在rendered属性中对其进行检查,如下所示:

So, in your view you can check it in the rendered attribute as follows:

<h:form rendered="#{not empty request.remoteUser}">
    <h:commandLink value="Logout" action="#{auth.logout}" />
</h:form>
<h:link value="Login" outcome="login" rendered="#{empty request.remoteUser}" />

另请参见:

  • 有条件地显示JSF组件
  • See also:

    • Conditionally displaying JSF components
    • 这篇关于如何检查用户是否登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆