Servlet过滤器无法正常工作

时间:2013-07-02 13:26:30

标签: java jsf servlets servlet-filters

我希望在未打开或关闭会话时重定向到开始页面。我添加了Servlet过滤器,但它无法正常工作。

我的 SessionFilter 类:

public class SessionFilter implements Filter {

    private ArrayList<String> urlList;

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        String url = request.getServletPath();
        HttpSession session = request.getSession(false);

        if (new Bean().getLoggedIn() || urlList.contains(url) || session != null){
            chain.doFilter(req, resp);
        }
        else{
            response.sendRedirect("index.jsf");
        }
    }

    public void init(FilterConfig config) throws ServletException {
        String urls = config.getInitParameter("avoid-urls");
        StringTokenizer token = new StringTokenizer(urls, ",");

        urlList = new ArrayList<String>();

        while (token.hasMoreTokens()) {
            urlList.add(token.nextToken());

        }
    }

    public void destroy() {
    }

}


我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
    <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>com.tsystems.demail.SessionFilter</filter-class>
        <init-param>
            <param-name>avoid-urls</param-name>
            <param-value>/index.jsf, /registration.jsf</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>SessionFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
</web-app>

当我登录时new Bean().getLoggedIn()为真。注销时 - false。如何更改我的过滤器?哪里有错误?

2 个答案:

答案 0 :(得分:1)

我认为你应该尝试:

 response.sendRedirect(request.getContextPath() + "/index.jsf");

而不是:

response.sendRedirect("index.jsf"); 

但请查看以下示例:

我假设在登录方法中,我将当前用户放在会话中,这样我就可以在用户连接时获取非null对象!

方法登录:

 ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();

 Map<String, Object> sessionMap = externalContext.getSessionMap();

 sessionMap.put("User", wantConnect);   

现在,如果用户尝试访问某些包含 url-pattern成员的页面,例如http://www.domain.com/member/ ....,则会调用以下过滤器。

@WebFilter("/member/*")  
public class RequestInterceptor implements Filter {

public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
        FilterChain chain) throws ServletException, IOException {

      HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        HttpSession session = request.getSession();

        if (session == null || session.getAttribute("User") == null) {
            response.sendRedirect(request.getContextPath() + "/index.xhtml"); // No logged-in user found, so redirect to login page.
        } else {
            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }




}

@Override
public void destroy() {
    // Cleanup global variables if necessary.
}

}

这是我的web.xml

<filter>
    <description>Requires user to log in as a member</description>
    <filter-name>RequestInterceptor</filter-name>
    <filter-class>fr.atoswg.peu.security.RequestInterceptor</filter-class>
</filter>
<filter-mapping>
    <filter-name>RequestInterceptor</filter-name>
    <url-pattern>/member/*</url-pattern>
</filter-mapping>

希望有帮助:)!

答案 1 :(得分:0)

请注意     response.sendRedirect(“index.jsf”)

相对路径作为其参数。因此,假设index.jsf位于Web应用程序的根目录中,您可能希望使用值“/index.jsf”(以正斜杠开头)来处理所有级别的重定向。如果没有,这样的网址:

/folder1/folder2/somePage.jsf

将被重定向到

/folder1/folder2/index.jsf

而不是

 /index.jsf