Created
November 27, 2014 14:45
-
-
Save SergioLarios/a4cf9752ab779ec07b7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hibu.chile.controller; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.servlet.ModelAndView; | |
import com.hibu.chile.constant.Mappings; | |
import com.hibu.chile.constant.Request; | |
import com.hibu.chile.constant.Templates; | |
import com.hibu.chile.form.LoginForm; | |
import com.hibu.chile.form.validator.LoginFormValidator; | |
import com.hibu.chile.interceptor.SecurityInterceptor; | |
import com.hibu.chile.util.Validator; | |
import com.hibu.chile.view.LoginView; | |
import com.hibu.chile.web.FormUtils; | |
import com.hibu.chile.web.LdapAuthenticator; | |
import com.hibu.chile.web.SpringUtils; | |
@Controller | |
public class LoginController { | |
/* ******************************************************* | |
* ****************** Login : /login ********************* | |
* *******************************************************/ | |
@RequestMapping(value = Mappings.LOGIN, method = RequestMethod.GET) | |
public ModelAndView loginGet(HttpServletRequest request, HttpServletResponse response) { | |
LoginView view = new LoginView(); | |
view.setForm(new LoginForm()); | |
view = SpringUtils.checkSuMsgs(request, view); | |
return SpringUtils.createMv(Templates.TMPL_LOGIN, Request.VIEW, view); | |
} | |
/* ******************************************************* | |
* ****************** Login : /login ******************** | |
* *******************************************************/ | |
@RequestMapping(value = Mappings.LOGIN, method = RequestMethod.POST) | |
public ModelAndView loginPost(HttpServletRequest request, HttpServletResponse response) | |
throws Exception { | |
LoginForm form = FormUtils.fillForm(request, LoginForm.class); | |
LoginView view = new LoginView(); | |
view.setForm(form); | |
validator.validate(form, request); | |
if (validator.isValid()) { | |
boolean loggedIn = ldapAuthenticator.login(form.getUser(), form.getPssw()); | |
if (loggedIn) { | |
HttpSession session = request.getSession(); | |
session.setAttribute(SecurityInterceptor.USER_NAME, form.getUser()); | |
String lastPath = (String) session.getAttribute(SecurityInterceptor.LAST_PATH); | |
if (Validator.isBlank(lastPath) || Mappings.LOGIN.equals(lastPath)) { | |
return SpringUtils.createRedirect(Mappings.HOME); | |
} | |
else { | |
return SpringUtils.createRedirect(lastPath); | |
} | |
} | |
else { | |
SpringUtils.addSuMsg(request, LOGIN_FAIL, false); | |
} | |
} | |
else { | |
SpringUtils.addSuMsgs(request, validator.getErrors(), false); | |
} | |
view = SpringUtils.checkSuMsgs(request, view); | |
return SpringUtils.createMv(Templates.TMPL_LOGIN, Request.VIEW, view); | |
} | |
/* ******************************************************* | |
* ******************* Logut: /logout ******************** | |
* *******************************************************/ | |
@RequestMapping(value = Mappings.LOGOUT, method = RequestMethod.GET) | |
public ModelAndView logoutGet(HttpServletRequest request, HttpServletResponse response) { | |
HttpSession session = request.getSession(); | |
session.setAttribute(SecurityInterceptor.USER_NAME, null); | |
return SpringUtils.createRedirect(Mappings.LOGIN); | |
} | |
/* ******************************* | |
****** Private constants ******** | |
****************************** */ | |
private static final String LOGIN_FAIL = "login.fail"; | |
/* ******************************* | |
******* Injected objects ******** | |
****************************** */ | |
private LdapAuthenticator ldapAuthenticator; | |
private LoginFormValidator validator = new LoginFormValidator(); | |
/* ******************************* | |
********** Constructor ********** | |
******************************* */ | |
@Autowired | |
public LoginController(LdapAuthenticator ldapAuthenticator) { | |
this.ldapAuthenticator = ldapAuthenticator; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment