簡體   English   中英

JSF重定向在過濾器中導致異常

[英]JSF Redirect causes exception in a filter

我已經為要保護的所有頁面配置了身份驗證篩選器。 但是,當它嘗試重定向到登錄頁面時,遇到以下錯誤

com.sun.faces.context.FacesFileNotFoundException

..這是我的過濾器

@WebFilter(filterName = "Authentication Filter", urlPatterns = { "/pages/*" }, dispatcherTypes = {
        DispatcherType.REQUEST, DispatcherType.FORWARD })
public class AuthenticationFilter implements Filter {
    static final Logger logger = Logger.getLogger(AuthenticationFilter.class);
    private String contextPath;

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;

        if (httpRequest.getUserPrincipal() == null) {
            httpResponse.sendRedirect(contextPath
                    + "/faces/pages/public/login.xhtml");
            return;
        }
        chain.doFilter(request, response);
    }
    public void init(FilterConfig fConfig) throws ServletException {
        contextPath = fConfig.getServletContext().getContextPath();
    }
}

..,並將我的web.xml與此人servlet的代碼映射

<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>

不確定,但我已驗證該路徑在我的項目文件夾中存在

+pages
    +public
        -login.xhtml

生成的路徑是

http://localhost:8080/MyApp/faces/pages/public/login.xhtml

有人知道原因嗎?

異常表明JSF無法找到視圖。 您的項目是否具有以下目錄結構:contextRoot / faces / pages / public / login.xhtml?

通常,某些IDE(即NetBeans)默認將/faces路徑前綴添加到faces url-pattern中。 您可能已從web.xml對其進行了更改,但尚未從過濾器sendRedirect參數中刪除它。

為了使您的過濾器正常工作,請從過濾器中的sendRedirect()方法中刪除/faces前綴:

httpResponse.sendRedirect(contextPath + "/pages/public/login.xhtml");

或像這樣將其添加到web.xml中:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

最后,請注意您的過濾器不會引起無限循環。 在重定向之前添加此檢查可能會很有用:

HttpServletRequest req = (HttpServletRequest) request;
if (!req.getRequestURI().contains("/pages/public/login.xhtml") && httpRequest.getUserPrincipal() == null) {
        // redirect
        return;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM