Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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 Android Security Essentials Live Lessons this week in the Android 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
Author

JSP way to access logged in Spring user

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

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="http://www.springframework.org/security/tags" %>

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP way to access logged in Spring user
 
Similar Threads
Security implementation - suggestions
Auxiliary tables saving
after advice to j_security check
Access spring user object
Passing the session to JSF