2014-09-01 119 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

前几天我有一个类似的问题。

这是不够的,检查会话是空的那种方式。你可能有一个绑定到该会话的对象。例如,我有用户。在登录页面中,我将用户对象绑定到会话。

检查该对象是否为空。不要使用会话。

因为浏览器可能创建了会话或会话可能是新的,但是您尚未将任何登录信息发送给它。