2013-07-02 166 views
1

當會話沒有打開或關閉時,我想重定向到起始頁面。我添加了Servlet過濾器,但它不起作用。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。如何更改我的過濾器?我哪裏有錯誤?

+3

什麼是豆子?你的意思是每創建一個新的*時間? –

+1

「但它不起作用」它不起作用,它根本不重定向,它重定向到錯誤的頁面,你得到一個HTTP 500,沒有任何反應,什麼?根據調試器的判斷,else塊中的代碼是否可以執行?請詳細說明,謝謝。 – Keith

+0

@DaveNewton我的Bean類:http://pastebin.com/EKU1d0EY – rubaka

回答

0

注意 response.sendRedirect是( 「index.jsf」)

需要相對路徑作爲其參數。因此,假設index.jsf位於Web應用程序的根目錄中,則可能需要使用值「/index.jsf」(以正斜槓開始)來處理各級別的重定向。如果沒有,像這樣的網址:

/folder1/folder2/somePage.jsf 

將被重定向到

/folder1/folder2/index.jsf 

,而不是

/index.jsf 
+0

我得到了相同的結果 – rubaka

+0

您是否已驗證您的else塊中的重定向行是否已實際到達?通過使用調試器或日誌語句? – Keith

+0

我確定沒關係 – rubaka

0

我想你應該嘗試:

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> 

希望它幫助:)!

相關問題