File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes JSP way to access logged in Spring user Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "JSP way to access logged in Spring user" Watch "JSP way to access logged in Spring user" New topic

JSP way to access logged in Spring user

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436

I'm using Spring 3.1.0.RELEASE and Spring security 3.1. Currently, to access my logged in user on my JSP pages, I set a model attribute in the controller ...

and then on my JSP pages, I can use commandName="${user}" in my form to set up a page where I can manipulate a user. Is there a pre-defined way of accessing the spring security user? Currently, I have to put the above code in all my controllers, which seems redundant.

Thanks, - Dave
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Yes, Spring Security has a tag library. So in your jsp you could do something like

<security:authentication property="principal.username"/></security:authorize>

You just need to include the tag lib in your jsp pages. Typically in a header.jsp that is imported/included in all your jsp pages if you need it showing on all pages.

<%@ taglib prefix="security" uri="" %>


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: JSP way to access logged in Spring user
It's not a secret anymore!