Reputation:
How can I programmatically know, in my web app, the username and password or user role of the user who is currently logged into tomcat on which the app is deployed?
Upvotes: 1
Views: 360
Reputation: 1109292
This information is available by HttpServletRequest
and inherently thus also ExternalContext
.
The following methods are available:
String username = externalContext.getRemoteUser();
UserPrincipal principal = externalContext.getUserPrincipal();
boolean admin = externalContext.isUserInRole("ADMIN");
You cannot get the password in any way for security reasons.
Note that the HttpServletRequest
is available as #{request}
in EL. So the following should also be possible:
<p>Welcome, #{request.remoteUser}</p>
<h:panelGroup id="adminPanel" rendered="#{request.isUserInRole('ADMIN')}">
...
</h:panelGroup>
Upvotes: 4