package info.bukova.isspst.security;

import info.bukova.isspst.SessionData;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

public class LoginFailHandler implements AuthenticationFailureHandler {

	@Autowired
	private SessionData sessionData;
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request,	HttpServletResponse response, AuthenticationException ex)
			throws IOException, ServletException {
		
		Logger logger = LoggerFactory.getLogger(LoginFailHandler.class);
		logger.info("Login failed with message: " + ex.getMessage());
		
		sessionData.setLoginFailed(true);
		
		response.sendRedirect("app/");

	}

}
