LoginEndpoint.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.maxkey.web.endpoint;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import javax.servlet.ServletException;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22. import org.maxkey.authn.AbstractAuthenticationProvider;
  23. import org.maxkey.authn.LoginCredential;
  24. import org.maxkey.authn.support.kerberos.KerberosService;
  25. import org.maxkey.authn.support.rememberme.AbstractRemeberMeService;
  26. import org.maxkey.authn.support.socialsignon.service.SocialSignOnProviderService;
  27. import org.maxkey.authn.support.wsfederation.WsFederationConstants;
  28. import org.maxkey.configuration.ApplicationConfig;
  29. import org.maxkey.constants.ConstantsStatus;
  30. import org.maxkey.domain.UserInfo;
  31. import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
  32. import org.maxkey.persistence.service.UserInfoService;
  33. import org.maxkey.util.StringUtils;
  34. import org.maxkey.web.WebConstants;
  35. import org.maxkey.web.WebContext;
  36. import org.maxkey.web.message.Message;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.beans.factory.annotation.Qualifier;
  41. import org.springframework.stereotype.Controller;
  42. import org.springframework.web.bind.annotation.CookieValue;
  43. import org.springframework.web.bind.annotation.ModelAttribute;
  44. import org.springframework.web.bind.annotation.PathVariable;
  45. import org.springframework.web.bind.annotation.RequestMapping;
  46. import org.springframework.web.bind.annotation.RequestParam;
  47. import org.springframework.web.bind.annotation.ResponseBody;
  48. import org.springframework.web.servlet.ModelAndView;
  49. /**
  50. * @author Crystal.Sea
  51. *
  52. */
  53. @Controller
  54. public class LoginEndpoint {
  55. private static Logger _logger = LoggerFactory.getLogger(LoginEndpoint.class);
  56. @Autowired
  57. @Qualifier("applicationConfig")
  58. ApplicationConfig applicationConfig;
  59. @Autowired
  60. @Qualifier("socialSignOnProviderService")
  61. SocialSignOnProviderService socialSignOnProviderService;
  62. @Autowired
  63. @Qualifier("remeberMeService")
  64. AbstractRemeberMeService remeberMeService;
  65. @Autowired
  66. @Qualifier("kerberosService")
  67. KerberosService kerberosService;
  68. @Autowired
  69. @Qualifier("userInfoService")
  70. UserInfoService userInfoService;
  71. /*@Autowired
  72. @Qualifier("wsFederationService")
  73. WsFederationService wsFederationService;*/
  74. @Autowired
  75. @Qualifier("authenticationProvider")
  76. AbstractAuthenticationProvider authenticationProvider ;
  77. @Autowired
  78. @Qualifier("tfaOtpAuthn")
  79. protected AbstractOtpAuthn tfaOtpAuthn;
  80. /*
  81. @Autowired
  82. @Qualifier("jwtLoginService")
  83. JwtLoginService jwtLoginService;
  84. */
  85. /**
  86. * init login
  87. * @return
  88. */
  89. @RequestMapping(value={"/login"})
  90. public ModelAndView login(
  91. HttpServletRequest request,
  92. HttpServletResponse response,
  93. @CookieValue(value=WebConstants.REMEBER_ME_COOKIE,required=false) String remeberMe,
  94. @RequestParam(value=WebConstants.CAS_SERVICE_PARAMETER,required=false) String casService,
  95. @RequestParam(value=WebConstants.KERBEROS_TOKEN_PARAMETER,required=false) String kerberosToken,
  96. @RequestParam(value=WebConstants.KERBEROS_USERDOMAIN_PARAMETER,required=false) String kerberosUserDomain,
  97. @RequestParam(value=WsFederationConstants.WA,required=false) String wsFederationWA,
  98. @RequestParam(value=WsFederationConstants.WRESULT,required=false) String wsFederationWResult) {
  99. _logger.debug("LoginController /login.");
  100. ModelAndView modelAndView = new ModelAndView("login");
  101. boolean isAuthenticated= WebContext.isAuthenticated();
  102. //for RemeberMe login
  103. if(!isAuthenticated){
  104. if(applicationConfig.getLoginConfig().isRemeberMe()&&remeberMe!=null&& !remeberMe.equals("")){
  105. _logger.debug("Try RemeberMe login ");
  106. isAuthenticated=remeberMeService.login(remeberMe,response);
  107. }
  108. }
  109. //for Kerberos login
  110. if(!isAuthenticated){
  111. if(applicationConfig.getLoginConfig().isKerberos()&&
  112. kerberosUserDomain!=null&&!kerberosUserDomain.equals("")&&
  113. kerberosToken!=null && !kerberosToken.equals("")){
  114. _logger.debug("Try Kerberos login ");
  115. isAuthenticated=kerberosService.login(kerberosToken,kerberosUserDomain);
  116. }
  117. }
  118. //for WsFederation login
  119. if(!isAuthenticated){
  120. if(applicationConfig.getLoginConfig().isWsFederation()&&
  121. StringUtils.isNotEmpty(wsFederationWA) &&
  122. wsFederationWA.equalsIgnoreCase(WsFederationConstants.WSIGNIN)){
  123. _logger.debug("Try WsFederation login ");
  124. //isAuthenticated=wsFederationService.login(wsFederationWA,wsFederationWResult,request);
  125. }
  126. }
  127. //for normal login
  128. if(!isAuthenticated){
  129. modelAndView.addObject("isRemeberMe", applicationConfig.getLoginConfig().isRemeberMe());
  130. modelAndView.addObject("isKerberos", applicationConfig.getLoginConfig().isKerberos());
  131. modelAndView.addObject("isMfa", applicationConfig.getLoginConfig().isMfa());
  132. if(applicationConfig.getLoginConfig().isMfa()) {
  133. modelAndView.addObject("otpType", tfaOtpAuthn.getOtpType());
  134. modelAndView.addObject("otpInterval", tfaOtpAuthn.getInterval());
  135. }
  136. if( applicationConfig.getLoginConfig().isKerberos()){
  137. modelAndView.addObject("userDomainUrlJson", kerberosService.buildKerberosProxys());
  138. }
  139. modelAndView.addObject("isCaptcha", applicationConfig.getLoginConfig().isCaptcha());
  140. modelAndView.addObject("sessionid", WebContext.getSession().getId());
  141. //modelAndView.addObject("jwtToken",jwtLoginService.buildLoginJwt());
  142. //load Social Sign On Providers
  143. if(applicationConfig.getLoginConfig().isSocialSignOn()){
  144. _logger.debug("Load Social Sign On Providers ");
  145. modelAndView.addObject("ssopList", socialSignOnProviderService.getSocialSignOnProviders());
  146. }
  147. }
  148. if(isAuthenticated){
  149. return WebContext.redirect("/forwardindex");
  150. }
  151. Object loginErrorMessage=WebContext.getAttribute(WebConstants.LOGIN_ERROR_SESSION_MESSAGE);
  152. modelAndView.addObject("loginErrorMessage", loginErrorMessage==null?"":loginErrorMessage);
  153. WebContext.removeAttribute(WebConstants.LOGIN_ERROR_SESSION_MESSAGE);
  154. return modelAndView;
  155. }
  156. @RequestMapping(value={"/logon.do"})
  157. public ModelAndView logon(
  158. HttpServletRequest request,
  159. HttpServletResponse response,
  160. @ModelAttribute("loginCredential") LoginCredential loginCredential) throws ServletException, IOException {
  161. authenticationProvider.authenticate(loginCredential);
  162. if (WebContext.isAuthenticated()) {
  163. return WebContext.redirect("/forwardindex");
  164. } else {
  165. return WebContext.redirect("/login");
  166. }
  167. }
  168. @RequestMapping("/login/{username}")
  169. @ResponseBody
  170. public HashMap <String,Object> queryLoginUserAuth(@PathVariable("username") String username) {
  171. UserInfo userInfo=new UserInfo();
  172. userInfo.setUsername(username);
  173. userInfo=userInfoService.load(userInfo);
  174. HashMap <String,Object> authnType=new HashMap <String,Object>();
  175. authnType.put("authnType", userInfo.getAuthnType());
  176. authnType.put("appLoginAuthnType", userInfo.getAppLoginAuthnType());
  177. return authnType;
  178. }
  179. @RequestMapping("/login/otp/{username}")
  180. @ResponseBody
  181. public String produceOtp(@PathVariable("username") String username) {
  182. UserInfo userInfo = new UserInfo();
  183. userInfo.setUsername(username);
  184. UserInfo queryUserInfo=userInfoService.loadByUsername(username);//(userInfo);
  185. if(queryUserInfo!=null) {
  186. tfaOtpAuthn.produce(queryUserInfo);
  187. return "ok";
  188. }
  189. return "fail";
  190. }
  191. /**
  192. * view register
  193. * @return
  194. */
  195. @RequestMapping(value={"/register"})
  196. public ModelAndView register(HttpServletRequest request,HttpServletResponse response) {
  197. _logger.debug("LoginController /register.");
  198. ModelAndView modelAndView = new ModelAndView("registration/register");
  199. Object loginErrorMessage=WebContext.getAttribute(WebConstants.LOGIN_ERROR_SESSION_MESSAGE);
  200. modelAndView.addObject("loginErrorMessage", loginErrorMessage==null?"":loginErrorMessage);
  201. WebContext.removeAttribute(WebConstants.LOGIN_ERROR_SESSION_MESSAGE);
  202. return modelAndView;
  203. }
  204. @RequestMapping(value={"/registeron"})
  205. @ResponseBody
  206. public Message registeron(UserInfo userInfo,@RequestParam String emailMobile) throws ServletException, IOException {
  207. if(StringUtils.isNullOrBlank(emailMobile)) {
  208. return new Message(WebContext.getI18nValue("register.emailMobile.error"),"1");
  209. }
  210. if(StringUtils.isValidEmail(emailMobile)) {
  211. userInfo.setEmail(emailMobile);
  212. }
  213. if(StringUtils.isValidMobileNo(emailMobile)) {
  214. userInfo.setMobile(emailMobile);
  215. }
  216. if(!(StringUtils.isValidEmail(emailMobile)||StringUtils.isValidMobileNo(emailMobile))) {
  217. return new Message(WebContext.getI18nValue("register.emailMobile.error"),"1");
  218. }
  219. UserInfo temp=userInfoService.queryUserInfoByEmailMobile(emailMobile);
  220. if(temp!=null) {
  221. return new Message(WebContext.getI18nValue("register.emailMobile.exist"),"1");
  222. }
  223. temp=userInfoService.loadByUsername(userInfo.getUsername());
  224. if(temp!=null) {
  225. return new Message(WebContext.getI18nValue("register.user.error"),"1");
  226. }
  227. userInfo.setStatus(ConstantsStatus.ACTIVE);
  228. if(userInfoService.insert(userInfo)) {
  229. return new Message(WebContext.getI18nValue("login.text.register.success"),"0");
  230. }
  231. return new Message(WebContext.getI18nValue("login.text.register.error"),"1");
  232. }
  233. }