If all you need to retrieve is the username,
getRequest().getRemoteUser() should be sufficient.

Matt

On 2/21/07, tonyl <[EMAIL PROTECTED]> wrote:

I need to get the current user and after searching forums and discussions and
examples about how to do this, I found that variations on the following code
seem to be considered a good way to get the current user:

      SecurityContext ctx = null;
      Authentication auth = null;

      if( ctx == null )
      {
         ctx = SecurityContextHolder.getContext();
         if ( auth == null && ctx.getAuthentication() != null )
            auth = ctx.getAuthentication();
      }

      if ( auth != null )
      {
         String currentUser;
         if ( auth.getPrincipal() instanceof UserDetails )
         {
            currentUser = ((UserDetails) auth.getPrincipal()).getUsername();
         }
         else
         {
            currentUser = String.valueOf( auth.getPrincipal() );
         }

         return currentUser;
      }

      But then, I did some more investigation and stumbled upon:

      getRequest().getUserPrincipal().getName();

      getRequest() returns an HttpServletRequest that is retrieved through a
FacesContext
      getFacesContext().getExternalContext().getRequest();

     So, which is the better way to get the current user? Personally, I like
using the request because it's a lot less code
     and the BasePage class generated by AppFuse already has the
getRequest() method implemented, but the majority
     of examples and forums used the first option through the
SecurityContext and Authentication object.

     Thanks,

     Tony


--
View this message in context: 
http://www.nabble.com/getting-current-logged-in-user-tf3264914s2369.html#a9075626
Sent from the AppFuse - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




--
http://raibledesigns.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to