2014-09-01 157 views
1

我在Netbeans 7.4中創建了一個java web-app MySQL作爲數據庫。我已經使用過濾器來控制未經授權的訪問,但它無法正常工作。 我有我的index.jsp(作爲登錄頁面),home.jsp和Add_Details.jsp文件。我希望當會話爲空或用戶嘗試訪問其他頁面(index.jsp除外)時,它們將重定向到index.jsp。但它不起作用。這裏是我的代碼: -Servlet過濾器無法正常工作

AuthenticationFilter.java

package Filters; 
//all mandatory files are imported. 

public class AuthenticationFilter implements Filter { 

    private ServletContext context; 

    @Override 
    public void init(FilterConfig fConfig) throws ServletException { 
     this.context = fConfig.getServletContext(); 
     this.context.log("AuthenticationFilter initialized"); 
    } 

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

     HttpServletRequest req = (HttpServletRequest) request; 
     HttpServletResponse res = (HttpServletResponse) response; 

     String uri = req.getRequestURI(); 
     this.context.log("Requested Resource::" + uri); 

     HttpSession session = req.getSession(false); 

     if (session == null && !(uri.endsWith("index.jsp") || !uri.endsWith("Bucket/") || !uri.endsWith("LoginServlet"))) 
     { 
      this.context.log("Unauthorized access request"); 
      res.sendRedirect("index.jsp"); 
     } else { 
      // pass the request along the filter chain 
      chain.doFilter(request, response); 
     } 

    } 

    @Override 
    public void destroy() { 
     // close any resources here 
    } 
} 

LogingRequestFilter.java

public class LoggingRequestFilter implements Filter { 

    private ServletContext context; 

    @Override 
    public void init(FilterConfig fConfig) throws ServletException { 
     this.context = fConfig.getServletContext(); 
     this.context.log("RequestLoggingFilter initialized"); 
    } 

    @Override 
    public void doFilter(ServletRequest request, ServletResponse response, 
      FilterChain chain) throws IOException, ServletException { 
     HttpServletRequest req = (HttpServletRequest) request; 
     Enumeration<String> params = req.getParameterNames(); 
     while (params.hasMoreElements()) { 
      String name = params.nextElement(); 
      String value = request.getParameter(name); 
      this.context.log(req.getRemoteAddr() + "::Request Params::{" + name 
        + "=" + value + "}"); 
     } 

     Cookie[] cookies = req.getCookies(); 
     if (cookies != null) { 
      for (Cookie cookie : cookies) { 
       this.context.log(req.getRemoteAddr() + "::Cookie::{" 
         + cookie.getName() + "," + cookie.getValue() + "}"); 
      } 
     } 
     // pass the request along the filter chain 
     chain.doFilter(request, response); 
    } 

    @Override 
    public void destroy() { 
     // we can close resources here 
    } 
} 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 
     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_3_0.xsd"> 

    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <filter> 
     <filter-name>LoggingRequestFilter</filter-name> 
     <filter-class>com.org.king.Filters.LoggingRequestFilter</filter-class> 
    </filter> 
    <filter> 
     <filter-name>AuthenticationFilter</filter-name> 
     <filter-class>com.org.king.Filters.AuthenticationFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>LoggingRequestFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>REQUEST</dispatcher> 
    </filter-mapping> 
    <filter-mapping> 
     <filter-name>AuthenticationFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <session-config> 
     <session-timeout> 
      30 
     </session-timeout> 
    </session-config> 
</web-app> 

回答

0

我會建議你的Servlet過濾器內設置一個斷點,並通過代碼走理解爲什麼執行的流程是不是你的「如果」塊中獲取:

if (session == null && !(uri.endsWith("index.jsp") || !uri.endsWith("Bucket/") || !uri.endsWith("LoginServlet"))) 
{ 

此外,如果要實現這個從從頭開始,您可能需要保持「DTSTTCPW」和「YAGNI」等原則。 * DTSTTCPW =您能夠工作的最簡單的事情 ** YAGNI =你不會需要它

捷克這一點: Java Filter to redirect users who are not logged in to login page

0

前幾天我有一個類似的問題。

這是不夠的,檢查會話是空的那種方式。你可能有一個綁定到該會話的對象。例如,我有用戶。在登錄頁面中,我將用戶對象綁定到會話。

檢查該對象是否爲空。不要使用會話。

因爲瀏覽器可能創建了會話或會話可能是新的,但是您尚未將任何登錄信息發送給它。

相關問題