ForgotPassword fix

This commit is contained in:
shimingxy
2020-04-18 16:02:22 +08:00
parent 8edf03d6eb
commit 8ba1b6966b
18 changed files with 255 additions and 345 deletions

View File

@@ -1,20 +1,16 @@
package org.maxkey.web.contorller;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.maxkey.config.ApplicationConfig;
import org.maxkey.dao.service.ForgotPasswordService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.maxkey.crypto.password.opt.AbstractOptAuthn;
import org.maxkey.dao.service.UserInfoService;
import org.maxkey.domain.ForgotPassword;
import org.maxkey.domain.UserInfo;
import org.maxkey.web.WebContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@@ -22,115 +18,96 @@ import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = { "/forgotpassword" })
public class ForgotPasswordContorller {
private static Logger _logger = LoggerFactory.getLogger(ForgotPasswordContorller.class);
@Autowired
ForgotPasswordService forgotPasswordService;
Pattern emailRegex = Pattern.compile(
"^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
Pattern mobileRegex = Pattern.compile(
"^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\\\d{8}$");
public class ForgotType{
public final static int NOTFOUND = 1;
public final static int EMAIL = 2;
public final static int MOBILE = 3;
}
public class PasswordResetResult{
public final static int SUCCESS = 1;
public final static int CAPTCHAERROR = 2;
public final static int PASSWORDERROR = 3;
}
@Autowired
private UserInfoService userInfoService;
@Autowired
protected ApplicationConfig applicationConfig;
@Qualifier("tfaMailOptAuthn")
protected AbstractOptAuthn tfaMailOptAuthn;
@Autowired
private PasswordEncoder passwordEncoder;
@Qualifier("tfaMobileOptAuthn")
protected AbstractOptAuthn tfaMobileOptAuthn;
@RequestMapping(value = { "/forward" })
public ModelAndView forwardreg() {
_logger.debug("Registration /forgotpassword/forward.");
return new ModelAndView("forgotpassword/forward");
_logger.debug("forgotpassword /forgotpassword/forward.");
return new ModelAndView("forgotpassword/findpwd");
}
@RequestMapping(value = { "/email" })
public ModelAndView email(@RequestParam String email) {
_logger.debug("Registration /forgotpassword/email.");
_logger.debug("email : " + email);
UserInfo userInfo = forgotPasswordService.queryUserInfoByEmail(email);
ModelAndView modelAndView = new ModelAndView("forgotpassword/email");
modelAndView.addObject("emailsend", 0);
modelAndView.addObject("email", email);
if (userInfo != null) {
ForgotPassword forgotPassword = new ForgotPassword();
forgotPassword.setId(forgotPassword.generateId());
forgotPassword.setEmail(email);
forgotPassword.setUid(userInfo.getId());
forgotPassword.setUsername(userInfo.getUsername());
forgotPasswordService.insert(forgotPassword);
HtmlEmail hemail = new HtmlEmail();
try {
hemail.setHostName(applicationConfig.getEmailConfig().getSmtpHost());
hemail.setSmtpPort(applicationConfig.getEmailConfig().getPort());
hemail.setAuthenticator(new DefaultAuthenticator(applicationConfig.getEmailConfig().getUsername(),
applicationConfig.getEmailConfig().getPassword()));
hemail.addTo(userInfo.getEmail(), userInfo.getNickName());
hemail.setFrom(applicationConfig.getEmailConfig().getSenderMail(), "ConnSec");
hemail.setSubject("ConnSec Cloud Identity & Access ReSet Password .");
// set the html message
String forgotPasswordUrl = WebContext.getHttpContextPath() + "/forgotpassword/resetpwd/"
+ forgotPassword.getId();
// set the html message
String emailText = "<html>";
emailText += "<a href='" + forgotPasswordUrl + "'>Reset Password</a><br>";
emailText += " or copy " + forgotPasswordUrl + " to brower.";
emailText += "</html>";
hemail.setHtmlMsg(emailText);
// set the alternative message
hemail.setTextMsg("Your email client does not support HTML messages");
// send the email
hemail.send();
modelAndView.addObject("emailsend", 1);
} catch (EmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@RequestMapping(value = { "/emailmobile" })
public ModelAndView email(@RequestParam String emailMobile,@RequestParam String captcha) {
_logger.debug("forgotpassword /forgotpassword/emailmobile.");
_logger.debug("emailMobile : " + emailMobile);
UserInfo userInfo = userInfoService.queryUserInfoByEmailMobile(emailMobile);
Matcher matcher = emailRegex.matcher(emailMobile);
int forgotType = ForgotType.NOTFOUND;
if (matcher.matches() && null != userInfo) {
tfaMailOptAuthn.produce(userInfo);
forgotType = ForgotType.EMAIL;
}
matcher = mobileRegex.matcher(emailMobile);
if (matcher.matches() && null != userInfo) {
tfaMobileOptAuthn.produce(userInfo);
forgotType = ForgotType.MOBILE;
}
ModelAndView modelAndView = new ModelAndView("forgotpassword/resetpwd");
modelAndView.addObject("userId", userInfo==null ?"":userInfo.getId());
modelAndView.addObject("username", userInfo==null ?"":userInfo.getUsername());
modelAndView.addObject("emailMobile", emailMobile);
modelAndView.addObject("forgotType", forgotType);
return modelAndView;
}
@RequestMapping(value = { "/resetpwd/{id}" })
public ModelAndView resetpwd(@PathVariable("id") String id) {
_logger.debug("Registration /forgotpassword/resetpwd.");
ForgotPassword forgotPassword = forgotPasswordService.get(id);
ModelAndView mav = new ModelAndView("forgotpassword/resetpwd");
if (forgotPassword != null) {
mav.addObject("model", forgotPassword);
}
return mav;
}
@RequestMapping(value = { "/setpassword/{id}" })
public ModelAndView setPassWord(@PathVariable("id") String id, @RequestParam String password,
@RequestParam String confirmpassword) {
_logger.debug("forgotPassword /forgotPassword/pwdreseted.");
@RequestMapping(value = { "/setpassword" })
public ModelAndView setPassWord(
@RequestParam String userId,
@RequestParam String username,
@RequestParam int forgotType,
@RequestParam String password,
@RequestParam String confirmpassword,
@RequestParam String captcha) {
_logger.debug("forgotPassword /forgotpassword/pwdreseted.");
ModelAndView modelAndView = new ModelAndView("forgotpassword/pwdreseted");
if (password.equals(confirmpassword)) {
ForgotPassword forgotPassword = forgotPasswordService.get(id);
if (forgotPassword != null) {
UserInfo userInfo = new UserInfo();
userInfo.setId(forgotPassword.getUid());
userInfo.setPassword(password);
userInfo.setDecipherable(password);
userInfo.setUsername(forgotPassword.getUsername());
if (null != password && password.equals(confirmpassword)) {
UserInfo userInfo = new UserInfo();
userInfo.setId(userId);
userInfo.setUsername(username);
userInfo.setPassword(password);
userInfo.setDecipherable(password);
if ((forgotType == ForgotType.EMAIL && tfaMailOptAuthn.validate(userInfo, captcha)) ||
(forgotType == ForgotType.MOBILE && tfaMobileOptAuthn.validate(userInfo, captcha))
) {
userInfoService.changePassword(userInfo);
forgotPasswordService.remove(id);
modelAndView.addObject("pwdreseted", 1);
modelAndView.addObject("passwordResetResult", PasswordResetResult.SUCCESS);
} else {
modelAndView.addObject("pwdreseted", 2);
modelAndView.addObject("passwordResetResult", PasswordResetResult.CAPTCHAERROR);
}
} else {
modelAndView.addObject("pwdreseted", 0);
modelAndView.addObject("passwordResetResult", PasswordResetResult.PASSWORDERROR);
}
return modelAndView;
}