Failing to redirect from JSF phaselistener

后端 未结 2 1587
既然无缘
既然无缘 2021-01-16 23:36

My problem is similar to the one here but doesn\'t involve PrimeFaces in any way. Also, I couldn\'t find a real solution there.

I\'m basically trying to

2条回答
  •  失恋的感觉
    2021-01-17 00:06

    Well I had the same problem like you but I did not solve in in such a complicated way as you are doing.My steps

    1) create a class that implements the PhaseListener
    import javax.faces.application.NavigationHandler;
    import javax.faces.context.FacesContext;
    import javax.faces.event.PhaseEvent;
    import javax.faces.event.PhaseId;
    import javax.faces.event.PhaseListener;
    
    /**
     *
     * @author fakumawah @date 09.03.2012
     */
    public class LoggedInCheck extends BackingBean implements PhaseListener
    {
    
    @Override`enter code here`
     public PhaseId getPhaseId()
      {
        return PhaseId.ANY_PHASE;
      }
    
      @Override
      public void beforePhase(PhaseEvent event)
      {
      }
    
      @Override
      public void afterPhase(PhaseEvent event)
      {
        FacesContext fc = event.getFacesContext();
        boolean loginPage = fc.getViewRoot().getViewId().lastIndexOf("login") > -1 ? true :     false;
        if (!loginPage && !loggedIn())
        {
          NavigationHandler nh = fc.getApplication().getNavigationHandler();
          nh.handleNavigation(fc, null, "gotologin");
        }
      }
    
      private boolean loggedIn()
      {
        return getSessionWrapper().isLoggedInAgain();
      }
    }
    

    Sorry for the uncommented code but I guess the code is really easy to understand. Most important thing to note is the afterPhase(..) which checks if I am in the logged in or if I have a session already. If I don´t it creates a navigator and navigates me to the login page

    2) Your isLoggedInAgain() should look something like this:

    /**
       * A method to check if Username is already logged in
       * @param username
       * @return 
       */
      public boolean isLoggedInAgain()
      {
        if (session != null) // Check if session is not null
        {
          if (session.isConnected()) // Check if session is connected   
          {
            return true;
            //destroyIfsLibrarySession();  // Session is available -> destroy session
          }
          else  // session is not available
          {
            logger.log(Level.SEVERE, "Already logged out");
            return false;
          }
        }
        return false;
      }
    

    Since I am dealing with LibrarySessions from Oracle CMSDK that is why my test for the session looks like this. Most important is to check your session somehow and give out true or false, depending on if session exist or not.

    3) Configure the Listener in faces-config.xml

    
    
        com.mycompany.mypackagename.webapp.LoggedInCheck
    
    

    4) Lastly define a navigation rule for the "gotologin"

    
        
            *
            
                gotologin
                /login.em
                
            
        
    

    And that is it, whenever you do not have a session on any page and are not on the login page, you will be carried to the login page.

    Enjoy

提交回复
热议问题