content: package org.thothlab.devilsvault.controllers.security;
  
  import java.io.IOException;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import org.springframework.security.authentication.BadCredentialsException;
  import org.springframework.security.authentication.LockedException;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.servlet.ModelAndView;
  import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  
  @Controller
  public class LoginController {
  	
  	@RequestMapping(value = "/", method = RequestMethod.GET)
  	public String login() throws IOException {
  		return "redirect:/login";
  	}
  
  	
  	@RequestMapping(value = "/login", method = RequestMethod.GET)
  	public ModelAndView login(@RequestParam(value = "error", required = false) String error,
  			@RequestParam(value = "logout", required = false) String logout, HttpServletRequest request) throws IOException {
  
  		ModelAndView model = new ModelAndView();
  		if (error != null) {
  			model.addObject("error", getErrorMessage(request, "SPRING_SECURITY_LAST_EXCEPTION"));
  		}
  
  		if (logout != null) {
  			model.addObject("msg", "You've been logged out successfully.");
  		}
  		
  		model.setViewName("LoginPage");
  
  		return model;
  
  	}
  
  	@RequestMapping(value="/logout", method = RequestMethod.GET)
  	public String logoutPage (HttpServletRequest request, HttpServletResponse response) {
  	    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  	    if (auth != null){    
  	        new SecurityContextLogoutHandler().logout(request, response, auth);
  	    }
  	    return "redirect:/login";
  	}
  	@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
  	public ModelAndView loginFailed (HttpServletRequest request, HttpServletResponse response) {
  	    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  	    if (auth != null){    
  	        new SecurityContextLogoutHandler().logout(request, response, auth);
  	    }
  	    ModelAndView model = new ModelAndView("LoginPage");
  		model.addObject("msg","Captcha Validation Failed !!");
  		return model;
  	}
  	
  	private String getErrorMessage(HttpServletRequest request, String key) {
  
  		Exception exception = (Exception) request.getSession().getAttribute(key);
  
  		String error = "";
  		if (exception instanceof BadCredentialsException) {
  			error = "Invalid username and password!";
  		} else if (exception instanceof LockedException) {
  			error = exception.getMessage();
  		} else {
  			error = "Invalid username and password!";
  		}
  
  		return error;
  	}
  	
  	@RequestMapping(value = "/captchafailed")
  	public String captcha_failed(RedirectAttributes redir,HttpServletRequest request, HttpServletResponse response) {
          String message = "Captcha verification failed !! Please try again !!";
          Authentication auth = SecurityContextHolder.getContext().getAuthentication();
  	    if (auth != null){    
  	        new SecurityContextLogoutHandler().logout(request, response, auth);
  	    }
          redir.addFlashAttribute("exception_message",message);
  	    return "redirect:/login";
  	}
  
  }
  