Search code examples
authenticationhttp-redirectjsf-2navigation

JSF 2 Navigation Rule redirect to home page if user is logged


I'm trying to accomplish redirection to home page when user go to login page and is already logged. This how I defined the rule:

<navigation-rule>
    <from-view-id>/login.xhtml</from-view-id>
    <navigation-case>
        <if>#{identity.loggedIn}</if>
        <to-view-id>/home.xhtml</to-view-id>
        <redirect />
    </navigation-case>
 </navigation-rule>

But I get the next exception:

WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-26) Error de argumento: el parámetro id es nulo: java.lang.NullPointerException: Error de argumento: el parámetro id es nulo
at com.sun.faces.util.Util.notNull(Util.java:425) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.flow.FlowHandlerImpl.getFlow(FlowHandlerImpl.java:89) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.NavigationHandlerImpl.determineViewFromActionOutcome(NavigationHandlerImpl.java:1204) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.NavigationHandlerImpl.findExactMatch(NavigationHandlerImpl.java:568) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.NavigationHandlerImpl.getViewId(NavigationHandlerImpl.java:462) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:189) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.NavigationHandlerImpl.handleNavigation(NavigationHandlerImpl.java:182) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:132) [jsf-impl-2.2.0-jbossorg-2.jar:]
at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0]
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0]
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0]
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.0-jbossorg-2.jar:]
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) [jsf-impl-2.2.0-jbossorg-2.jar:]
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) [jboss-jsf-api_2.2_spec-2.2.0.jar:2.2.0]
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:87) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:130) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.websockets.jsr.JsrWebSocketFilter.doFilter(JsrWebSocketFilter.java:138) [undertow-websockets-jsr-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:56) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:82) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:115) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.security.handlers.AuthenticationConstraintHandler.handleRequest(AuthenticationConstraintHandler.java:51) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.security.ServletSecurityConstraintHandler.handleRequest(ServletSecurityConstraintHandler.java:55) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:65) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:70) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at org.wildfly.extension.undertow.security.SecurityContextCreationHandler.handleRequest(SecurityContextCreationHandler.java:54)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:185) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:172) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:56) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:107) [undertow-servlet-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.server.HttpHandlers.executeRootHandler(HttpHandlers.java:36) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:629) [undertow-core-1.0.0.Beta3.jar:1.0.0.Beta3]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_25]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_25]

The redirection is released but the exception is raised. How can be solved?


Solution

  • After checking the source code the problem is related to outcome:

    the com.sun.faces.flow.FlowHandlerImpl.getFlow method need Id to be not null:

    public Flow getFlow(FacesContext context, String definingDocumentId, String id) {
        Util.notNull("context", context);
        Util.notNull("definingDocumentId", definingDocumentId);
        Util.notNull("id", id);
    

    The com.sun.faces.application.NavigationHandlerImpl:

    result.isFlowEntryFromExplicitRule = null != fh.getFlow(ctx, toFlowDocumentId, outcome);
    

    So the outcome is null and exception is rised. In my case I don't have outcome.

    If is needed from-outcome then in some place has to be ejecuted an action. To accomplish that the login.xhtml page need a f:viewAction.

    Implementing all this now my the requested use case is working well. If from-action is declared then the from-outcome is optional and vice versa.

    If the same logic of redirection is required when user go to root folder (domain/appname/) the only thing that has to be done is to add welcome-file to web.xml.

    <welcome-file>login.jsf</welcome-file>
    

    Finally this is the navigation rule declaration

    <navigation-rule>
        <from-view-id>/login.xhtml</from-view-id>
        <navigation-case>
            <!--optional<from-action>#{bean.loggedInNavigationRuleAction}</from-action>-->
            <from-outcome>logged</from-outcome>
            <if>#{identity.loggedIn}</if>
            <to-view-id>/home.xhtml</to-view-id>
            <redirect />
        </navigation-case>
     </navigation-rule>
    

    And this has to be added to login.xhtml

    <f:metadata>
      <f:viewParam name="x" value="#{bean.x}"/>
      <f:viewAction action="#{bean.loggedInNavigationRuleAction}" phase="APPLY_REQUEST_VALUES"/>
    </f:metadata>