Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain logged in user name in JSF when using container managed authentication

I have configured a form based log in in my JSF 2.0 Application on GlassFish 3 server, using a custom Realm. What is the simpliest way of getting information about a logged in user, i.e. the users name.

Is this possible? Or is the current session just associated with a security role? If so, is there some way of making this possible without changing the log in configuration?

Simply put, what I want is to display a simple message like:

Logged in as username

on my webpages.

like image 296
Tobbe Brolin Avatar asked Dec 29 '10 23:12

Tobbe Brolin


2 Answers

The easiest way to get at the logged in user via EL (Expression Language) in JSF 2.0 would be:

#{request.remoteUser}

Tobbe's answer would work well to get at the remote user from within a backing bean.

like image 141
Brian Leathem Avatar answered Nov 02 '22 15:11

Brian Leathem


The simple (maybe not the best) answer was:

FacesContext.getCurrentInstance().getExternalContext().getRemoteUser()

I'm shocked about how long it took me to figure that out.

like image 26
Tobbe Brolin Avatar answered Nov 02 '22 13:11

Tobbe Brolin