How to web filter in JSF 2?

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 a filter to the web.xml file:

 <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 continues to give me this error:

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

And after that my richfaces no longer work.

How can i solve this? Or create a web filter correctly?

+6
source share
1 answer

Any relative path URL (i.e. URLs that do not start with / ) that you pass to sendRedirect() will refer to the URI of the current request. 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 does not exist. View the URL in the address bar of the browser again.

To resolve this issue, instead redirect the URL rather than the URL with / .

 res.sendRedirect(req.getContextPath() + "/login.xhtml"); 
+9
source

Source: https://habr.com/ru/post/900608/


All Articles