jsf check user logged in

2019-05-19 04:37发布

问题:

How can I check if a user is logged in using jsf?

This is how I am doing it and it's not working:

<ui:fragment rendered="#{!request.getUserPrincipal().getName().equalsIgnoreCase('anonymous')}">
    <h:outputLink value="logout">
        <h:outputText value="Logout" />
    </h:outputLink>
</ui:fragment>

回答1:

Just check if HttpServletRequest#getUserPrincipal() is not null.

<ui:fragment rendered="#{request.userPrincipal != null}">
    <h:outputLink value="logout">
        <h:outputText value="Logout" />
    </h:outputLink>
</ui:fragment>

It also works for HttpServletRequest#getRemoteUser().

<ui:fragment rendered="#{request.remoteUser != null}">
    <h:outputLink value="logout">
        <h:outputText value="Logout" />
    </h:outputLink>
</ui:fragment>