I've a filter used to intercept all my requests to check the validity of my logged in user within the session before process to the target. But the problem is that it keeps redirecting to the login page when trying to log in ?
Reference used : https://stackoverflow.com/questions/13274279/authentication-filter-and-servlet-for-login
filter
public class AuthenticationFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Filter init method()");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Filter doFilter method()");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession();
Users loggedIn = (Users) session.getAttribute("LoggedInUser");
boolean loggedInUser = session != null && session.getAttribute("LoggedInUser") != null;
String logInURI = req.getContextPath() + "/loginPage.jsp";
boolean loginRequest = req.getRequestURI().equals(logInURI);
if (loggedInUser || loginRequest) {
chain.doFilter(request, response);
} else {
res.sendRedirect(logInURI);
}
}
@Override
public void destroy() {
}
}
web.xml
<filter>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>