0

I want to redirect my users to a change password page as long as they don't have changed their password. This state is available from a SessionScoped Bean.

How can I globally modify all links and outcomes so that a user who has to change his password is redirected to the change password page?

This should work even if they try to access pages through their URLs.

Can this be achieved with JSF?

Thanks!

t3chris
  • 1,390
  • 1
  • 15
  • 25
  • 3
    A servlet Filter comes to mind. Basic servlet stuff you know, JSF has nothing to do with it. – Gimby Feb 21 '14 at 09:14
  • See `ServletFilter` example [here](http://stackoverflow.com/questions/21440904/how-to-call-bean-method-on-every-page-request/21471739#21471739) – Vasil Lukach Feb 22 '14 at 18:57

2 Answers2

1

You should normally use ServletFilter for that. You can access your ManagedBean there, as it is available in HttpSession.

Ben Goldin
  • 589
  • 2
  • 7
1

Probably You can achieve this with phase listeners. For example:

public class MyPhaseListener implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent event) {
        System.out.println("afterPhase with ID : " + event.getPhaseId());
    }

    @Override
    public void beforePhase(PhaseEvent event) {
        // Do not exceute subsequent phases if sending a redirect
        event.getFacesContext().responseComplete();
        try {
            ((HttpServletResponse)event.getFacesContext().getExternalContext().getResponse()).sendRedirect("http://stackoverflow.com");
        } catch (IOException ex) {
            Logger.getLogger(MyPhaseListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }
}

Register the listener in faces-config.xml

<faces-config version="2.2"
              xmlns="http://xmlns.jcp.org/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
    <lifecycle>
        <phase-listener>MyPhaseListener</phase-listener>
    </lifecycle>

</faces-config>
Stefan
  • 576
  • 7
  • 27
  • 1
    You can see here http://stackoverflow.com/a/2633733/1126380 how to get the session bean to check if the user change the password. – Arturo Volpe Feb 21 '14 at 10:50