permaculture playing cards
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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
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: 17260

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 - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
It is sorta covered in the JavaRanch Style Guide.
subject: JSP way to access logged in Spring user