Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP way to access logged in Spring user

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic