提交 169167f4 authored 作者: Whispa's avatar Whispa

commit commit

上级 592edc89
...@@ -44,6 +44,12 @@ ...@@ -44,6 +44,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId> <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
......
...@@ -6,13 +6,15 @@ import com.example.afrishop_v3.models.ResetBody; ...@@ -6,13 +6,15 @@ import com.example.afrishop_v3.models.ResetBody;
import com.example.afrishop_v3.models.TbCfUserInfo; import com.example.afrishop_v3.models.TbCfUserInfo;
import com.example.afrishop_v3.repository.UserRepository; import com.example.afrishop_v3.repository.UserRepository;
import com.example.afrishop_v3.security.services.AuthenticationUser; import com.example.afrishop_v3.security.services.AuthenticationUser;
import com.example.afrishop_v3.util.SMSUtil; import com.example.afrishop_v3.util.*;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.net.URLDecoder;
import java.util.Date; import java.util.Date;
import java.util.Optional; import java.util.Optional;
...@@ -22,12 +24,14 @@ public class UserController extends Controller { ...@@ -22,12 +24,14 @@ public class UserController extends Controller {
private final UserRepository repository; private final UserRepository repository;
private final AuthenticationUser user; private final AuthenticationUser user;
private final PasswordEncoder encoder; private final PasswordEncoder encoder;
private final EmailHelper emailHelper;
private static Logger logger = LoggerFactory.getLogger(UserController.class); private static Logger logger = LoggerFactory.getLogger(UserController.class);
public UserController(UserRepository repository, AuthenticationUser user, PasswordEncoder encoder) { public UserController(UserRepository repository, AuthenticationUser user, PasswordEncoder encoder, EmailHelper emailHelper) {
this.repository = repository; this.repository = repository;
this.user = user; this.user = user;
this.encoder = encoder; this.encoder = encoder;
this.emailHelper = emailHelper;
} }
@PutMapping("bindPhoneOrEmail") @PutMapping("bindPhoneOrEmail")
...@@ -36,9 +40,16 @@ public class UserController extends Controller { ...@@ -36,9 +40,16 @@ public class UserController extends Controller {
if( StringUtils.isNotBlank(email) ){ if( StringUtils.isNotBlank(email) ){
if( isEmailValid(email) ) user.setEmail(email); boolean detect = StringUtils.isNotBlank(code) && code.equals(user.getVerificationCode());
if( isEmailValid(email) && detect ){
else return new Result(ResultCodeEnum.ILLEGAL_ARGUMENT.getCode(),"Email address is not valid"); if( user.getEmail() != null && user.getEmail().equals(user.getFirebaseUid())){
user.setFirebaseUid(email);
}
user.setEmail(email);
}
else return new Result(ResultCodeEnum.ILLEGAL_ARGUMENT.getCode(),detect ?"Email address is not valid" : "Code verification error");
} }
...@@ -58,6 +69,24 @@ public class UserController extends Controller { ...@@ -58,6 +69,24 @@ public class UserController extends Controller {
} }
@GetMapping("/getEmailCode")
public Result getEmailCode(@RequestParam("email") String email){
try {
EmailTemplateBo emailTemplateBo = new EmailTemplateBo();
String name = URLDecoder.decode(email, Consts.UTF_8.name());
Integer randomCode = RandomCodeHelper.producedRandomCode(6);
emailTemplateBo.setNick(name);
emailTemplateBo.setIdentifyCode(randomCode);
Integer identifyCode = emailHelper.sendIdentifyEmail(email, EmailTemplateConstant.REGISTER, emailTemplateBo);
TbCfUserInfo user = this.user.user();
user.setVerificationCode(String.valueOf(identifyCode));
repository.save(user);
return new Result("Verification code has been sent");
}catch (Exception e){
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),e.getMessage());
}
}
@GetMapping("/identifyCode/{phone}") @GetMapping("/identifyCode/{phone}")
public Result getUserPhoneCode(@PathVariable("phone") String phone) { public Result getUserPhoneCode(@PathVariable("phone") String phone) {
......
package com.example.afrishop_v3.util;
import com.example.afrishop_v3.config.DomainProperties;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.http.Consts;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Component("emailHelper")
public class EmailHelper {
private final Configuration configuration;
private final DomainProperties domainProperties;
private static Logger logger = LoggerFactory.getLogger(EmailHelper.class);
public EmailHelper(Configuration configuration, DomainProperties domainProperties) {
this.configuration = configuration;
this.domainProperties = domainProperties;
}
public Integer sendIdentifyEmail(String toEmail, String templateName, EmailTemplateBo emailTemplateBo) throws IOException, TemplateException, EmailException {
/*Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
// 初始化一些配置
cfg.setDirectoryForTemplateLoading(new File("/"));
cfg.setDefaultEncoding(Consts.UTF_8.name());
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);*/
Template t = configuration.getTemplate(templateName);
Map<String,Object> model = new HashMap<>();
model.put("identifyCode", emailTemplateBo.getIdentifyCode());
model.put("nick", emailTemplateBo.getNick());
String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
HtmlEmail email = new HtmlEmail();//创建一个HtmlEmail实例对象
email.setHostName(domainProperties.getProperty("email.hostName"));//邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
email.setSslSmtpPort("465");
email.setSSLOnConnect(true);
email.setCharset(Consts.UTF_8.name());//设置发送的字符类型
//email.addTo("zhengfanguang@163.com");//设置收件人
email.addTo(toEmail);//设置收件人
email.setFrom(domainProperties.getProperty("email.from.email"), domainProperties.getProperty("email.from.name"));//发送人的邮箱为自己的,用户名可以随便填
email.setAuthentication(domainProperties.getProperty("email.authentication.userName"), domainProperties.getProperty("email.authentication.password"));//设置发送人的邮箱和用户名和授权码(授权码是自己设置的)
email.setSubject(domainProperties.getProperty("email.subject"));//设置发送主题
email.setMsg(html);//设置发送内容
String res=email.send();
//logger.info("邮件发送结果:"+res);
return emailTemplateBo.getIdentifyCode();
}
public void sendEmail(String email, String templateName) throws IOException, TemplateException, EmailException {
Template t = configuration.getTemplate(templateName);
Map<String[],Object> model = new HashMap<>();
String html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
HtmlEmail emails = new HtmlEmail();//创建一个HtmlEmail实例对象
emails.setHostName(domainProperties.getProperty("email.hostName"));//邮箱的SMTP服务器,一般123邮箱的是smtp.123.com,qq邮箱为smtp.qq.com
emails.setCharset(Consts.UTF_8.name());//设置发送的字符类型
//email.addTo("zhengfanguang@163.com");//设置收件人
emails.addTo(email);//设置收件人
emails.setFrom(domainProperties.getProperty("email.from.email"), domainProperties.getProperty("email.from.name"));//发送人的邮箱为自己的,用户名可以随便填
emails.setAuthentication(domainProperties.getProperty("email.authentication.userName"), domainProperties.getProperty("email.authentication.password"));//设置发送人的邮箱和用户名和授权码(授权码是自己设置的)
emails.setSubject(domainProperties.getProperty("email.project"));//设置发送主题
emails.setMsg(html);//设置发送内容
String res=emails.send();
if(res!=null){
// tbCfUserInfoService.updateIsSend(email);
}
logger.info("邮件发送结果:"+res);
}
}
package com.example.afrishop_v3.util;
/**
* templates/email-template.ftl 对应的参数
*/
public class EmailTemplateBo {
//用户名
private String nick;
//验证码
private Integer identifyCode;
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
public Integer getIdentifyCode() {
return identifyCode;
}
public void setIdentifyCode(Integer identifyCode) {
this.identifyCode = identifyCode;
}
}
package com.example.afrishop_v3.util;
/**
* 发送email所用的模板文件
*/
public class EmailTemplateConstant {
//注册
public static String REGISTER="email-template-a.ftl";
//注册
public static String REGISTERS="email-template-a.ftl";
//20191227
public static String REGISTER20191227="email-template-a.ftl";
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论