The answer to your question greatly depends upon your implementation, but I'll give you an example from my evolving implementation.
Conceptually, the ContextHolder should contain a SecureContext or some derivative. With that being the case, you can get the SecureContext instance and then query it for its authentication. The authentication may or may not exist. If it does exist, then you can query the authentication to get its principal. The returned value of this call is either a String, a UserDetails instance, or some other instance of a class that you have defined (something that has implemented UserDetails and extended beyond).
In my case, I have a User class which implements UserDetails and extends this functionality to provide additional user profile details (e.g. name, last name, etc.).
So basically that's how you'd get to your principle information. Next you have a choice of where to put this accessing code. You could put these gory details in your JSP page, but that is probably not the preferred method. What I have done is written a method which exists in an abstract controller which basically returns a user and then my controller returns this instance as part of the model. The method could be refactored into a utilities class for more widespread use and I may do that once things settle out.
Anyway, here's the method:
Code:
/**
* Retrieves a User instance from a secure context. If there is no authentication
* returns null.
* @return the authenticated User instance or null if not authenticated.
*/
protected User getUserFromContext()
{
if ((ContextHolder.getContext() == null)
|| !(ContextHolder.getContext() instanceof SecureContext)
|| (((SecureContext) ContextHolder.getContext()).getAuthentication() == null))
{
return null;
}
Authentication auth = ((SecureContext) ContextHolder.getContext()).getAuthentication();
if (auth.getPrincipal() == null)
{
return null;
}
else if (auth.getPrincipal() instanceof User)
{
return (User) auth.getPrincipal();
}
else
{
return null;
}
}
And the controller code which uses this:
Code:
public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws Exception
{
return new ModelAndView(memberView, USER, getUserFromContext());
}
And this allows your JSP code to do this:
Code:
<%@ include file="/WEB-INF/views/inc/taglibs.jsp" %>
<html>
<head>
<title>Member Home</title>
</head>
<body>
Welcome ${user.name} ${user.lastName}
</body>
</html>
Hope this helps, but be careful as you are not guaranteed to have a User in your authentication. If you have anonymous authentication in play, this could complicate things. Also, I did not add any null checking within the JSP, so you'd probably add that. But fundamentally, this is representative of code that could be evolved to work.
Bill