Skip to content

Instantly share code, notes, and snippets.

@Mithrandir0x
Created February 22, 2012 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mithrandir0x/1883548 to your computer and use it in GitHub Desktop.
Save Mithrandir0x/1883548 to your computer and use it in GitHub Desktop.
package edu.ub.gpr.statistics.filters;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
/**
*
* @author oriol.lopez
*/
public class SessionAuthentication implements Filter
{
private static String authType;
Logger log = Logger.getLogger(SessionAuthentication.class);
public void init(FilterConfig filterConfig) throws ServletException
{
try
{
Properties config = new Properties();
config.load(this.getClass().getClassLoader().getResourceAsStream("gprstats.properties"));
PropertyConfigurator.configure(config);
authType = config.getProperty("gprstats.authentication");
log.debug("SessionAuthentication Filter has been initialized.");
}
catch ( Exception ex )
{
log.error("Error " + ex.getMessage(), ex);
}
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest _request = (HttpServletRequest)request;
HttpServletResponse _response = (HttpServletResponse)response;
log.debug("New request filtered: " + _request.getRequestURI());
if ( _request.getRequestURI().equals("/gprstats/login.jsp") ||
_request.getRequestURI().equals("/gprstats/login") ||
_request.getRequestURI().equals("/gprstats/logout") ||
_request.getRequestURI().equals("/gprstats/guest.jsp") )
{
chain.doFilter(request, response);
}
else
{
if ( authType.equals("NONE") )
{
chain.doFilter(request, response);
}
else if ( authType.equals("SIMPLE") )
{
HttpSession session = _request.getSession();
String logged = (String)session.getAttribute("logged");
if ( logged == null )
{
_response.sendRedirect("/gprstats/login.jsp");
}
else
{
chain.doFilter(request, response);
}
}
else if ( authType.equals("AUTENUB") )
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
}
public void destroy()
{
log.info("SessionAuthentication Filter will be destroyed.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment