0

I´m using omnifaces and prmifaces to make a login and trying to implement a webFilter from omnifaces. But i always get a 404 and nothing is happenning. Can someone please help me! This is my web.xml.

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <!-- Change to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>icarus-red</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.FONT_AWESOME</param-name>
        <param-value>true</param-value>
    </context-param>
    <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>pt.isec.pd.pgs.login.LoginFilter</filter-class>
    </filter>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>login.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

and this is my LoginFilter class.

@WebFilter("/secured/*")
public class LoginFilter extends HttpFilter {

    @Inject
    private LoginBackingBean loginBackingBean;

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

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException {
        System.out.println("doFilter");
        String loginURL = request.getContextPath() + "/login.xhtml";

        boolean loggedIn = (session != null) && (session.getAttribute("user") != null);
        boolean loginRequest = request.getRequestURI().equals(loginURL);
        boolean resourceRequest = Servlets.isFacesResourceRequest(request);

        if (loggedIn || loginRequest || resourceRequest) {
            if (!resourceRequest) { // Prevent browser from caching restricted resources. See also https://stackoverflow.com/q/4194207/157882
                Servlets.setNoCacheHeaders(response);
            }

            chain.doFilter(request, response); // So, just continue request.
        } else {
            Servlets.facesRedirect(request, response, loginURL);
        }
    }
}

i have a /login.xhtml and a /secured/home.xhtml. my backingBean is LoginBackingBean.

What am i doing wrong!? If someone need any more info, please let me know. Because i´m quite frustrating. Thanx you in advance.

PcS
  • 38
  • 8
  • Question is ambiguous. First of all, what's the URL you see in the browser address bar of the 404 error page? – BalusC Feb 13 '18 at 20:13
  • Sorry! The URL was http:myVmIp:port/central-presentation/login.xhtml. I'm using payara as the app server inside a vbox linux machine. – PcS Feb 13 '18 at 22:53
  • And without the filter, the very same URL works fine when copypasted into browser's address bar? – BalusC Feb 14 '18 at 07:08
  • Yes it does. It Works even without the "/login.xhtml" part. – PcS Feb 14 '18 at 12:54

1 Answers1

0

Change my approach to a "default" java LoginFilter. And it solved my problema. this is my filter class

@WebFilter("/secured/*")
public class LoginFilter implements Filter {

final static Logger LOGGER = Logger.getLogger(LoginFilter.class);

 @Inject
 private LoginBackingBean loginBean;

@Override
public void init(FilterConfig filterConfig) throws ServletException {
LOGGER.debug("init()");}

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

LOGGER.debug("doFilter() -> LoggedIn: " + loginBean.isLoggedIn());
    if (loginBean == null || !loginBean.isLoggedIn()) {
        LOGGER.debug("to login!");
        String contextPath = ((HttpServletRequest) request).getContextPath();
        ((HttpServletResponse) response).sendRedirect(contextPath + "/login.xhtml");
    }
    chain.doFilter(request, response);
}

@Override
public void destroy() {
    LOGGER.debug("destroy()");
   }
}

This is my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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-app_3_1.xsd">
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Production</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>icarus-red</param-value>
</context-param>
<context-param>
    <param-name>primefaces.FONT_AWESOME</param-name>
    <param-value>true</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>LoginFilter</filter-name>
    <filter-class>pt.isec.pd.pgs.login.LoginFilter</filter-class>
</filter>
<welcome-file-list>
    <welcome-file>secured/home.xhtml</welcome-file>
</welcome-file-list>

Any doubts you can get in touch.

PcS
  • 38
  • 8
  • Looks like OmniFaces isn't installed at all. The server startup logs should have hinted that the HttpFilter class couldn't be found and therefore the filter couldn't be initialized. Didn't you notice that? – BalusC Mar 24 '18 at 15:42