6

I create this filter :

public class LoginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest) request;
        HttpSession session = req.getSession();

        if (session.getAttribute("authenticated") != null || req.getRequestURI().endsWith("login.xhtml")) {
            chain.doFilter(request, response);
        } else {
            HttpServletResponse res = (HttpServletResponse) response;
            res.sendRedirect("login.xhtml");
            return;
        }

    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {
    }
}

This is my structure:

enter image description here

And then I add the filter in the web.xml:

<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>LoginFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

The filter works as it should but keeps giving me this error:

"Was not possible find or provider the resource, login"

And after that my richfaces doesn't works anymore.

How can I solve that ? Or create a web filter correctly ?

Valter Silva
  • 16,446
  • 52
  • 137
  • 218
  • That's a weird error message. Did you translate it from another language? Is it just a HTTP 404 error? – BalusC Nov 02 '11 at 12:01
  • I do BalusC, I'm from Brazil, so the error appears to me in portuguese, do you know how do I change the Eclipse to English ? So I could post the original error. – Valter Silva Nov 02 '11 at 12:02
  • 2
    Eclipse's default language is dependent on platform default locale. So if your OS is set to Portuguese, then Eclipse will inherit this setting. But you can override this by specifying `-nl [languagecode]` argument on `eclipse.exe`. E.g.: `eclipse.exe -nl en` will set it to English. – BalusC Nov 02 '11 at 12:05

1 Answers1

9

Any path-relative URL (i.e. URLs which do not start with /) which you pass to sendRedirect() will be relative to the current request URI. I understand that the login page is at http://localhost:8080/contextname/login.xhtml. So, if you for example access http://localhost:8080/contextname/pages/user/some.xhtml, then this redirect call will actually point to http://localhost:8080/contextname/pages/user/login.xhtml, which I think don't exist. Look at the URL in your browser address bar once again.

To fix this problem, rather redirect to a domain-relative URL instead, i.e. start the URL with /.

res.sendRedirect(req.getContextPath() + "/login.xhtml");
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555