LoginEndpoint.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.maxkey.web.endpoint;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.maxkey.authn.BasicAuthentication;
  5. import org.maxkey.authn.RealmAuthenticationProvider;
  6. import org.maxkey.authn.support.jwt.JwtLoginService;
  7. import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
  8. import org.maxkey.config.ApplicationConfig;
  9. import org.maxkey.web.WebConstants;
  10. import org.maxkey.web.WebContext;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.beans.factory.annotation.Qualifier;
  15. import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
  16. import org.springframework.security.web.savedrequest.RequestCache;
  17. import org.springframework.security.web.savedrequest.SavedRequest;
  18. import org.springframework.stereotype.Controller;
  19. import org.springframework.web.bind.annotation.CookieValue;
  20. import org.springframework.web.bind.annotation.ModelAttribute;
  21. import org.springframework.web.bind.annotation.RequestMapping;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.servlet.ModelAndView;
  24. /**
  25. * @author Crystal.Sea
  26. *
  27. */
  28. @Controller
  29. public class LoginEndpoint {
  30. private static Logger _logger = LoggerFactory.getLogger(LoginEndpoint.class);
  31. @Autowired
  32. @Qualifier("applicationConfig")
  33. protected ApplicationConfig applicationConfig;
  34. @Autowired
  35. @Qualifier("remeberMeService")
  36. protected AbstractRemeberMeService remeberMeService;
  37. @Autowired
  38. @Qualifier("jwtLoginService")
  39. JwtLoginService jwtLoginService;
  40. @Autowired
  41. @Qualifier("authenticationProvider")
  42. RealmAuthenticationProvider authenticationProvider ;
  43. /**
  44. * init login
  45. * @return
  46. */
  47. @RequestMapping(value={"/login"})
  48. public ModelAndView login(
  49. HttpServletRequest request,
  50. HttpServletResponse response,
  51. @CookieValue(value=WebConstants.REMEBER_ME_COOKIE,required=false) String remeberMe,
  52. @RequestParam(value = WebConstants.JWT_TOKEN_PARAMETER, required = false) String jwt) {
  53. _logger.debug("LoginController /login.");
  54. ModelAndView modelAndView = new ModelAndView();
  55. boolean isAuthenticated= WebContext.isAuthenticated();
  56. //for jwt Login
  57. if(!isAuthenticated){
  58. if(jwt!=null&&!jwt.equals("")){
  59. isAuthenticated=jwtLoginService.login(jwt, response);
  60. }
  61. }
  62. //for RemeberMe login
  63. if(!isAuthenticated){
  64. if(applicationConfig.getLoginConfig().isRemeberMe()&&remeberMe!=null&& !remeberMe.equals("")){
  65. isAuthenticated=remeberMeService.login(remeberMe,response);
  66. }
  67. }
  68. //for normal login
  69. if(!isAuthenticated){
  70. modelAndView.addObject("isRemeberMe", applicationConfig.getLoginConfig().isRemeberMe());
  71. modelAndView.addObject("isCaptcha", applicationConfig.getLoginConfig().isCaptcha());
  72. modelAndView.addObject("sessionid", WebContext.getSession().getId());
  73. }
  74. //save first protected url
  75. SavedRequest firstSavedRequest = (SavedRequest)WebContext.getAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER);
  76. if(firstSavedRequest==null){
  77. RequestCache requestCache = new HttpSessionRequestCache();
  78. SavedRequest savedRequest =requestCache.getRequest(request, response);
  79. if(savedRequest!=null){
  80. _logger.debug("first request parameter "+savedRequest.getRedirectUrl());
  81. WebContext.setAttribute(WebConstants.FIRST_SAVED_REQUEST_PARAMETER, savedRequest);
  82. }
  83. }else {
  84. WebContext.setAttribute(WebConstants.SPRING_PROCESS_SAVED_REQUEST, firstSavedRequest);
  85. }
  86. modelAndView.setViewName("login");
  87. return modelAndView;
  88. }
  89. @RequestMapping(value={"/logon.do"})
  90. public ModelAndView logon(@ModelAttribute("authentication") BasicAuthentication authentication) {
  91. authenticationProvider.authenticate(authentication);
  92. if(WebContext.isAuthenticated()){
  93. return WebContext.redirect("/main");
  94. }else{
  95. return WebContext.redirect("/login");
  96. }
  97. }
  98. }