浏览代码

ForgotPassword Message Template

shimingxy 5 年之前
父节点
当前提交
0f63b6a775

+ 28 - 5
maxkey-core/src/main/java/org/maxkey/crypto/password/opt/impl/MailOtpAuthn.java

@@ -1,5 +1,6 @@
 package org.maxkey.crypto.password.opt.impl;
 
+import java.text.MessageFormat;
 import org.apache.commons.mail.DefaultAuthenticator;
 import org.apache.commons.mail.Email;
 import org.apache.commons.mail.SimpleEmail;
@@ -15,7 +16,10 @@ public class MailOtpAuthn extends AbstractOptAuthn {
     
     @Autowired
     EmailConfig emailConfig;
-
+    String subject = "One Time PassWord";
+    
+    String messageTemplate = "{0} You Token is {1} , it validity in {2}  minutes.";
+    
     public MailOtpAuthn() {
         optType = OptTypes.EMAIL;
     }
@@ -32,13 +36,15 @@ public class MailOtpAuthn extends AbstractOptAuthn {
                     new DefaultAuthenticator(emailConfig.getUsername(), emailConfig.getPassword()));
             
             email.setFrom(emailConfig.getSenderMail());
-            email.setSubject("One Time PassWord");
-            email.setMsg("You Token is " + token 
-                    + " , it validity in " + (interval / 60) + " minutes");
+            email.setSubject(subject);
+            email.setMsg(
+                    MessageFormat.format(
+                            messageTemplate,userInfo.getUsername(),token,(interval / 60)));
+            
             email.addTo(userInfo.getEmail());
             email.send();
             _logger.debug(
-                    "token " + token + " send to user +" + userInfo.getUsername() 
+                    "token " + token + " send to user " + userInfo.getUsername() 
                     + ", email " + userInfo.getEmail());
             //成功返回
             this.optTokenStore.store(
@@ -62,4 +68,21 @@ public class MailOtpAuthn extends AbstractOptAuthn {
         this.emailConfig = emailConfig;
     }
 
+    public String getSubject() {
+        return subject;
+    }
+
+    public void setSubject(String subject) {
+        this.subject = subject;
+    }
+
+    public String getMessageTemplate() {
+        return messageTemplate;
+    }
+
+    public void setMessageTemplate(String messageTemplate) {
+        this.messageTemplate = messageTemplate;
+    }
+    
+
 }

+ 23 - 10
maxkey-web-maxkey/src/main/java/org/maxkey/web/contorller/ForgotPasswordContorller.java

@@ -6,6 +6,8 @@ import java.util.regex.Pattern;
 import org.maxkey.crypto.password.opt.AbstractOptAuthn;
 import org.maxkey.dao.service.UserInfoService;
 import org.maxkey.domain.UserInfo;
+import org.maxkey.web.WebConstants;
+import org.maxkey.web.WebContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -30,6 +32,7 @@ public class ForgotPasswordContorller {
         public final static int NOTFOUND = 1;
         public final static int EMAIL = 2;
         public final static int MOBILE = 3;
+        public final static int CAPTCHAERROR = 4;
     }
     
     public class PasswordResetResult{
@@ -60,17 +63,27 @@ public class ForgotPasswordContorller {
     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;
+        UserInfo userInfo = null;
+        if (captcha != null && captcha
+                .equals(WebContext.getSession().getAttribute(
+                                WebConstants.KAPTCHA_SESSION_KEY).toString())) {
+            userInfo = userInfoService.queryUserInfoByEmailMobile(emailMobile);
+            Matcher matcher = emailRegex.matcher(emailMobile);
+            
+            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;
+            }
+           
+        }else {
+            _logger.debug("login captcha valid error.");
+            forgotType = ForgotType.CAPTCHAERROR;
         }
         
         ModelAndView modelAndView = new ModelAndView("forgotpassword/resetpwd");

+ 13 - 7
maxkey-web-maxkey/src/main/resources/templates/views/forgotpassword/resetpwd.ftl

@@ -10,13 +10,19 @@
 	<#include "../layout/nologintop.ftl">
 </div>
 <div class="container">	
-  <#if 1 == forgotType>
-  	<@locale code="forgotpassword.resetpwd.notfound.prefix"/> 
-  	<b>${emailMobile} </b>
-  	<@locale code="forgotpassword.resetpwd.notfound.suffix"/>
-  	<a href="javascript:history.go(-1);"><@locale code="forgotpassword.backstep"/></a >
-  </#if>
-  <#if 2 == forgotType || 3 == forgotType >
+
+
+	<#if 4 == forgotType>
+  		<@locale code="forgotpassword.pwdreseted.captcha"/> 
+  		<a href="javascript:history.go(-1);"><@locale code="forgotpassword.backstep"/></a >
+  	</#if>
+  	<#if 1 == forgotType>
+	  	<@locale code="forgotpassword.resetpwd.notfound.prefix"/> 
+	  	<b>${emailMobile} </b>
+	  	<@locale code="forgotpassword.resetpwd.notfound.suffix"/>
+	  	<a href="javascript:history.go(-1);"><@locale code="forgotpassword.backstep"/></a >
+  	</#if>
+  	<#if 2 == forgotType || 3 == forgotType >
 
 	<form action="<@base/>/forgotpassword/setpassword" method="post">
 		<table  class="table table-bordered">