在JSF 2中如何进行Web过滤? [英] How do a web filter in JSF 2?

查看:74
本文介绍了在JSF 2中如何进行Web过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了此过滤器:

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:

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 ?

推荐答案

传递给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.

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.

要解决此问题,请重定向到相对于域的URL,即以/开头.

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

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

这篇关于在JSF 2中如何进行Web过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆