I am using the org.omnifaces.filter.HttpFilter
to redirect visitors on login page when nobody is logged in.
@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, HttpSession session, FilterChain chain) throws ServletException, IOException {
String loginUrl = "/myapp/login?redirect_url=" + req.getRequestURL();
boolean loggedIn = (req.getRemoteUser() != null);
if (loggedIn) {
chain.doFilter(req, res); // So, just continue request.
} else {
Servlets.facesRedirect(req, res, loginUrl);
}
}
I want to redirect not logged in users to /login?redirect_url=previous_page_url
The problem is that all my URLs are beautified by pretty-faces and when I try to get the previous URL with HttpServletRequest.getRequestURI()
, it gives me the ugly URL.
For example, I configured an url /myapp/my-page-3
which displays /views/module1/page3.xhtml
.
But HttpServletRequest.getRequestURI()
is giving me /views/module1/page3.xhtml
and not /myapp/my-page-3
.
Any ideas ?
When the servlet based URL rewrite engine uses under the covers RequestDispatcher#forward()
to forward an incoming friendly-URL request to the desired resource, then you can use request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI)
to find out the original request URI.
String originalRequestURI = request.getAttribute(RequestDispatcher.FORWARD_REQUEST_URI);
As you're already using OmniFaces, you can use Servlets#getRequestURI()
to automatically detect it and return it when present, else fall back to the default HttpServletRequest#getRequestURI()
.
String requestURI = Servlets.getRequestURI(request);