Announcement Announcement Module
No announcement yet.
currentUser is null for some pages Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • currentUser is null for some pages

    I'm running the booking sample. But I found something very strange.
    After login, I could see Welcome, XXX on /spring/main
    But if I go to /spring/intro page, it shows the login link again. It looks like currentUser is empty on intro page.

    Those 2 pages use the same template, I can't understand why currentUser is empty on intor page.
    <c:if test="${not empty}">
      Welcome, ${} | <a href="${request.contextPath}/logout">Logout</a>
    <c:if test="${empty}">
       <a href="${request.contextPath}/login">Login</a>

  • #2
    After search in the Forum, I found this is an issue with mix-use of spring mvc and web flow. CurrentUser is only accessable in flow pages.

    In order to access currentUser, you have to write an interceptor.

    Here is mine:
    public class UserPrincipalInterceptor extends HandlerInterceptorAdapter {
    	protected final Log logger = LogFactory.getLog(getClass());
    	public void postHandle(HttpServletRequest request,
    			HttpServletResponse response, Object handler,
    			ModelAndView modelAndView) throws Exception {
    		if (modelAndView != null) {
    			Authentication auth = SecurityContextHolder.getContext()
    			modelAndView.addObject("currentUser", auth);
    And the config:
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    		<property name="order" value="1"/>
    		<property name="flowRegistry" ref="flowRegistry" />
    		<property name="defaultHandler">
    			<!-- If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named "intro" -->	
    			<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
    		<property name="interceptors">
    				<ref bean="userPrincipalInterceptor" />