1

我想在会话未打开或关闭时重定向到起始页。我添加了 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()是真的。注销时 - 错误。如何更改我的过滤器?我哪里有错误?

4

2 回答 2

1

我认为你应该尝试:

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

代替 :

response.sendRedirect("index.jsf"); 

但请检查以下示例:

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

方法登录:

 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>

希望对您有所帮助:)!

于 2013-07-09T07:28:45.967 回答
0

注意 response.sendRedirect("index.jsf")

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

/folder1/folder2/somePage.jsf

将被重定向到

/folder1/folder2/index.jsf

代替

 /index.jsf
于 2013-07-02T13:39:21.743 回答