NOTE: I don't want to redirect to a custom 401 page, I just want to use the default one.
I tried following the answer in this question:
How to "throw" JSF2 404 error?
With the following code:
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseStatus(401);
facesContext.responseComplete();
But it does not redirect at all. It just continues on.
I also tried:
externalContext.responseSendError(HttpServletResponse.SC_UNAUTHORIZED, "Page not found.");
But that did nothing either.
EDIT: I think the problem is that the redirection is not immediate. Before my code "returns" to the JSF lifecycle, it throws an exception.
Is there a way to redirect immediately rather than wait?