WebFilter使用omnifaces获得404而不工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebFilter使用omnifaces获得404而不工作相关的知识,希望对你有一定的参考价值。

我正在使用omnifaces和prmifaces进行登录并尝试从omnifaces实现webFilter。但我总是得到404,没有任何事情发生。有人可以帮帮我吗!这是我的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>

这是我的LoginFilter类。

@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);
        }
    }
}

我有/login.xhtml和/secured/home.xhtml。我的backingBean是LoginBackingBean。

我究竟做错了什么!?如果有人需要更多信息,请告诉我。因为我很沮丧。提前谢谢你。

答案

将我的方法更改为“默认”java登录过滤器。它解决了我的问题。这是我的过滤器类

@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()");
   }
}

这是我的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>

有任何疑问,你可以联系。

以上是关于WebFilter使用omnifaces获得404而不工作的主要内容,如果未能解决你的问题,请参考以下文章

@WebFilter("")配置servlet访问出现404的原因

org.omnifaces.facesviews.FacesViewsForwardingFilter 处的 NullPointerException

org.omnifaces.VetoAnnotatedTypeExtension.processAnnotatedType(@Observes ProcessAnnotatedType) 接收所有带注

在较新版本的 JSF 中,@FacesValidator 和 @FacesConverter 中的 EJB 和 CDI 注入点无法通过 OmniFaces 工作

Omnifaces CDN 资源处理程序中的 EL 表达式未在 Wildfly 9 中解析

为啥使用 react-admin 获得 404 的 API 端点?