如何检查用户是否登录? [英] How to check if is user logged in?
问题描述
我想在用户未登录时显示登录链接,并在用户登录时显示注销链接.我正在使用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
.
我该如何实现?
推荐答案
HttpServletRequest#getRemoteUser()
.两者均可通过#{facesContext.externalContext.remoteUser}
和#{request.remoteUser}
在EL中使用.如果它是null
,则表示该用户尚未登录.
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组件
- Conditionally displaying JSF components
See also:
这篇关于如何检查用户是否登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!