Announcement Announcement Module
Collapse
No announcement yet.
currentUser is null for some pages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.
    Code:
    <c:if test="${not empty currentUser.name}">
      Welcome, ${currentUser.name} | <a href="${request.contextPath}/logout">Logout</a>
    </c:if>
    <c:if test="${empty currentUser.name}">
       <a href="${request.contextPath}/login">Login</a>
    </c:if>

  • #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:
    Code:
    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()
    					.getAuthentication();
    
    			modelAndView.addObject("currentUser", auth);
    		}
    	}
    
    }
    And the config:
    Code:
    <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>
    		<property name="interceptors">
    			<list>
    				<ref bean="userPrincipalInterceptor" />
    			</list>
    		</property>
    	</bean>

    Comment

    Working...
    X