2

我有一个具有这种结构的项目(我无法更改):

Web Pages
   META-INF
   WEB-INF
   assets (javascript, css and images)
   includes (top.xhtml, bottom.xhtml, etc)
   templates (master.xhtml)
   views
      fornecedor
         -home.xhtml
         -user.xhtml
         -login.xhtml
      franqueador
         -home.xhtml
         -user.xhtml
         -login.xhtml

Ologin.xhtml每个文件夹都有一个原因,我不能更改它,它是由项目经理通过的。

这是我的会话过滤器:

@WebFilter("/views/*")
public class SessionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession(false);
        LoginController loginController = (LoginController) ((boolean) (session != null) ? session.getAttribute("loginController") : null);
        if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml")) {
            if (loginController == null || !loginController.getIsLoggedIn()) {
                response.sendRedirect(request.getContextPath()
                        + "/views/index.xhtml");
            } else {
                chain.doFilter(req, res);
            }
        }
    }


}

它不起作用,返回一个没有 html 代码的空白页面,当我更改为时.xhtml.html它给了我一个重定向循环。

我需要避开我login.xhtml的所有页面和index.xhtmlonviews文件夹。我已经调试了if我的过滤器,但它总是返回false

编辑

按照 BalusC 的回答,我得出了这个结论:

if (!request.getRequestURI().endsWith("/views/index.html")
                && !request.getRequestURI().endsWith("/views/fornecedor/login.html")
                && !request.getRequestURI().endsWith("/views/franqueado/login.html")
                && (loginController == null || !loginController.getIsLoggedIn())) {
            response.sendRedirect(request.getContextPath() + "/views/index.html");

        } else {
            chain.doFilter(req, res);
        }

它正在工作,但还有另一个问题,如果我有 10 个文件夹,我需要将它们添加到此if语句中......我需要使其自动。

4

1 回答 1

2

你的第一个if条件没有else。这解释了空白页。

您需要在一个单一的情况下评估 URL 和登录条件if

if (!request.getRequestURI().endsWith("/fornecedor/index.xhtml") && (loginController == null || !loginController.getIsLoggedIn()) {
    response.sendRedirect(request.getContextPath() + "/views/index.xhtml");
} else {
    chain.doFilter(req, res);
}

至于重定向循环,那是因为您FacesServlet被映射到 URL 模式*.html而不是*.xhtml出于某种不清楚的原因。所以请求 URL 永远不会匹配。相应地修复过滤器中的 URL。要对所有请求进行概括,login.htmlindex.html执行以下操作:

if (!request.getRequestURI().endsWith("/index.html") 
    && !request.getRequestURI().endsWith("/login.html") 
    && (loginController == null || !loginController.getIsLoggedIn())
{
    response.sendRedirect(request.getContextPath() + "/views/index.html");
} else {
    chain.doFilter(req, res);
}
于 2012-05-02T17:17:13.753 回答