java - Servlet 过滤器不工作

标签 java jsf servlets servlet-filters

我想在 session 未打开或关闭时重定向到起始页。我添加了 Servlet Filter,但它不起作用。

我的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。如何更换我的过滤器?我哪里出错了?

最佳答案

我认为你应该尝试:

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

而不是:

response.sendRedirect("index.jsf"); 

但是请检查下面的示例:

我假设在登录方法中,我将当前用户放入 session 中,以便在用户连接时可以获得非空对象!

方法登录:

 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>

希望对您有所帮助:)!

关于java - Servlet 过滤器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426872/

相关文章:

java - Java监视器和线程并发

javascript - 将 javascript 库添加/导入到 JSF+Maven 项目中

jsf - 从 JSF 中的托管 bean 获取资源文件的路径

session - JSF - 访问 SessionScoped 托管 bean

apache - HttpServletRequest 和 Apache 的问题

java - 具有子类和泛型的双向多对一

java - 多部分请求无法按 Spring 集成的预期工作

java - 生成增量 SPARQL 查询

java - 如何在简单的jsp文件上传中设置临时目录相对路径? FIleNotFound 异常

tomcat - OpenShift:上传文件到服务器