JSF 2 中的 Web 过滤器如何进行?

发布于 2024-12-13 09:39:28 字数 1445 浏览 5 评论 0原文

我创建这个过滤器:

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() {
    }
}

这是我的结构:

在此处输入图像描述

然后我在 web.xml 中添加过滤器:

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

过滤器按其应有的方式工作,但一直给我这个错误:

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

此后我的 Richfaces 不再工作。

我该如何解决这个问题?或者正确创建网页过滤器?

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 the filter in the web.xml:

<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 keeps giving me this error:

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

And after that my richfaces doesn't works anymore.

How can I solve that ? Or create a web filter correctly ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2024-12-20 09:39:28

传递给 sendRedirect() 的任何路径相关 URL(即不以 / 开头的 URL)都将相对于当前请求 URI。据我所知,登录页面位于 http://localhost:8080/contextname/login.xhtml。因此,如果您访问 http://localhost:8080/contextname/pages/user/some.xhtml,那么这个重定向调用实际上会指向http://localhost:8080/contextname/pages/user/login.xhtml,我认为没有存在。再次查看浏览器地址栏中的 URL。

要解决此问题,请重定向到与域相关的 URL,即以 / 开头的 URL。

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

Any path-relative URL (i.e. URLs which do not start with /) which you pass to sendRedirect() will be relative to the current request URI. 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 don't exist. Look at the URL in your browser address bar once again.

To fix this problem, rather redirect to a domain-relative URL instead, i.e. start the URL with /.

res.sendRedirect(req.getContextPath() + "/login.xhtml");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文