-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bde571c
commit 128522b
Showing
6 changed files
with
192 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.qzx.xdupartner.config; | ||
|
||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Component | ||
@ConfigurationProperties(prefix = "mail") | ||
public class MailConfig { | ||
|
||
private List<MailProperties> configs; | ||
|
||
@Data | ||
public static class MailProperties { | ||
|
||
/** | ||
* 密码 | ||
*/ | ||
private String username; | ||
|
||
/** | ||
* 密码 | ||
*/ | ||
private String password; | ||
|
||
/** | ||
* host | ||
*/ | ||
private String host; | ||
|
||
/** | ||
* 端口 | ||
*/ | ||
private Integer port; | ||
|
||
/** | ||
* 协议 | ||
*/ | ||
private String protocol; | ||
|
||
/** | ||
* 默认编码 | ||
*/ | ||
private String defaultEncoding; | ||
|
||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/qzx/xdupartner/config/MailSenderConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.qzx.xdupartner.config; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
@Slf4j | ||
@Component | ||
@AllArgsConstructor | ||
public class MailSenderConfig { | ||
|
||
private final MailConfig mailConfig; | ||
|
||
private final List<JavaMailSenderImpl> senderList; | ||
|
||
/** | ||
* 初始化 sender | ||
*/ | ||
@PostConstruct | ||
public void buildMailSender(){ | ||
List<MailConfig.MailProperties> mailConfigs = mailConfig.getConfigs(); | ||
log.info("初始化mailSender"); | ||
mailConfigs.forEach(mailProperties -> { | ||
|
||
// 邮件发送者 | ||
JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); | ||
javaMailSender.setDefaultEncoding(mailProperties.getDefaultEncoding()); | ||
javaMailSender.setHost(mailProperties.getHost()); | ||
javaMailSender.setPort(mailProperties.getPort()); | ||
javaMailSender.setProtocol(mailProperties.getProtocol()); | ||
javaMailSender.setUsername(mailProperties.getUsername()); | ||
javaMailSender.setPassword(mailProperties.getPassword()); | ||
|
||
// 添加数据 | ||
senderList.add(javaMailSender); | ||
}); | ||
} | ||
|
||
/** | ||
* 获取MailSender | ||
* @return CustomMailSender | ||
*/ | ||
public JavaMailSenderImpl getSender(){ | ||
if(senderList.isEmpty()){ | ||
buildMailSender(); | ||
} | ||
// 随机返回一个JavaMailSender | ||
return senderList.get(new Random().nextInt(senderList.size())); | ||
} | ||
|
||
/** | ||
* 清理 sender | ||
*/ | ||
public void clear(){ | ||
senderList.clear(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,20 @@ | |
|
||
import cn.hutool.core.date.DateUtil; | ||
import cn.hutool.core.util.ObjectUtil; | ||
import cn.hutool.core.util.ReUtil; | ||
import cn.hutool.core.util.StrUtil; | ||
import cn.hutool.json.JSONUtil; | ||
import com.qzx.xdupartner.constant.RedisConstant; | ||
import com.qzx.xdupartner.entity.User; | ||
import com.qzx.xdupartner.entity.vo.R; | ||
import com.qzx.xdupartner.entity.vo.ResultCode; | ||
import com.qzx.xdupartner.service.MailService; | ||
import com.qzx.xdupartner.service.UserService; | ||
import com.qzx.xdupartner.util.VerCodeGenerateUtil; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.lang3.RegExUtils; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.mail.MailException; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
|
@@ -37,46 +41,23 @@ public class VerifyController { | |
@Resource | ||
UserService userService; | ||
@Resource | ||
JavaMailSenderImpl mailSender; | ||
MailService mailService; | ||
|
||
@ApiOperation("") | ||
@GetMapping("/sendCode") | ||
public R<String> sendCode(@RequestParam("stuId") String stuId) { | ||
if (!(StrUtil.isNumeric(stuId) && stuId.length() == 11)) { | ||
return new R<>(ResultCode.STU_ID_ERROR); | ||
} | ||
String verCode = VerCodeGenerateUtil.getVerCode(); | ||
//一下为发送邮件部分 | ||
MimeMessage mimeMessage = null; | ||
MimeMessageHelper helper = null; | ||
try { | ||
String time = DateUtil.format(new Date(), "yyyy-MM-dd hh:mm:ss"); | ||
//发送复杂的邮件 | ||
mimeMessage = mailSender.createMimeMessage(); | ||
//组装 | ||
try { | ||
helper = new MimeMessageHelper(mimeMessage, true); | ||
} catch (MessagingException e) { | ||
throw new RuntimeException(e); | ||
} | ||
//邮件标题 | ||
helper.setSubject("【仙电搭子】 注册账号验证码"); | ||
//因为设置了邮件格式所以html标签有点多,后面的ture为支持识别html标签 | ||
//想要不一样的邮件格式,百度搜索一个html编译器,自我定制。 | ||
helper.setText("<h3>\n" + "\t<span style=\"font-size:16px;\">亲爱的用户:</span> \n" + "</h3>\n" + "<p>\n" + "\t<span style=\"font-size:14px;\"> </span><span style=\"font-size:14px;" + "\"> <span style=\"font-size:16px;\"> 您正在进行邮箱验证,本次请求的验证码为:<span " + "style=\"font-size:24px;color:#FFE500;\">" + verCode + "</span>,本验证码10分钟内有效,请在10" + "分钟内完成验证。(请勿泄露此验证码)如非本人操作,请忽略该邮件。(这是一封自动发送的邮件,请不要直接回复)</span></span>\n" + "</p>\n" + "<p style=\"text-align:right;\">\n" + "\t<span style=\"background-color:#FFFFFF;font-size:16px;color:#000000;\"><span " + "style=\"color:#000000;font-size:16px;background-color:#FFFFFF;\"><span class=\"token string\" " + "style=\"font-family:"font-size:16px;color:#000000;line-height:normal !important;" + "background-color:#FFFFFF;\">仙电搭子社区</span></span></span> \n" + "</p>\n" + "<p style=\"text-align:right;\">\n" + "\t<span style=\"background-color:#FFFFFF;font-size:14px;\"><span style=\"color:#FF9900;" + "font-size:18px;\"><span class=\"token string\" style=\"font-family:"font-size:16px;" + "color:#000000;line-height:normal !important;\"><span style=\"font-size:16px;color:#000000;" + "background-color:#FFFFFF;\">" + time + "</span><span style=\"font-size:18px;color:#000000;" + "background-color:#FFFFFF;\"></span></span></span></span> \n" + "</p>", true); | ||
//收件人 | ||
helper.setTo(stuId + "@stu.xidian.edu.cn"); | ||
//发送方 | ||
helper.setFrom("[email protected]"); | ||
//发送邮件 | ||
mailSender.send(mimeMessage); | ||
} catch (javax.mail.MessagingException e) { | ||
// 发送失败--服务器繁忙 | ||
return new R<>(ResultCode.MAIL_SEND_ERROR, "发送失败-服务器繁忙"); | ||
} catch (MailException e) { | ||
//邮箱是无效的,或者发送失败 | ||
return new R<>(ResultCode.MAIL_SEND_ERROR, "发送失败-邮箱无效"); | ||
boolean isSend = mailService.sendMail(stuId, verCode); | ||
if (isSend) { | ||
//发送验证码成功 | ||
stringRedisTemplate.opsForValue().set(RedisConstant.MAIL_CODE_PREFIX + stuId, verCode, 10, | ||
TimeUnit.MINUTES); | ||
return new R<>(ResultCode.SUCCESS, "发送成功"); | ||
} | ||
//发送验证码成功 | ||
stringRedisTemplate.opsForValue().set(RedisConstant.MAIL_CODE_PREFIX + stuId, verCode, 10, TimeUnit.MINUTES); | ||
return new R<>(ResultCode.SUCCESS, "发送成功"); | ||
return new R<>(ResultCode.MAIL_SEND_ERROR); | ||
} | ||
|
||
@ApiOperation("") | ||
|
@@ -99,8 +80,14 @@ private R<String> getStringR(String sessionKey, User user) { | |
return null; | ||
} | ||
user.setSessionKey(sessionKey); | ||
stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + sessionKey, JSONUtil.toJsonStr(user), RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); | ||
stringRedisTemplate.opsForValue().set(RedisConstant.LOGIN_PREFIX + sessionKey, JSONUtil.toJsonStr(user), | ||
RedisConstant.LOGIN_VALID_TTL, TimeUnit.DAYS); | ||
return new R<>(ResultCode.SUCCESS, sessionKey); | ||
} | ||
|
||
public static void main(String[] args) { | ||
if ((StrUtil.isNumeric("21009200334") && "21009200334".length() == 11)) { | ||
System.out.println("23444"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.qzx.xdupartner.service; | ||
|
||
public interface MailService { | ||
|
||
/** | ||
* 发送邮件 | ||
* @return 返回 true 或者 false | ||
*/ | ||
|
||
boolean sendMail(String stuId, String verCode); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/com/qzx/xdupartner/service/impl/MailServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.qzx.xdupartner.service.impl; | ||
|
||
import com.qzx.xdupartner.config.MailSenderConfig; | ||
import com.qzx.xdupartner.service.MailService; | ||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.mail.SimpleMailMessage; | ||
import org.springframework.mail.javamail.JavaMailSenderImpl; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Objects; | ||
|
||
@Slf4j | ||
@Service | ||
@AllArgsConstructor | ||
public class MailServiceImpl implements MailService { | ||
|
||
private final MailSenderConfig senderConfig; | ||
|
||
@Override | ||
public boolean sendMail(String stuId, String verCode) { | ||
//一下为发送邮件部分 | ||
|
||
JavaMailSenderImpl mailSender = senderConfig.getSender(); | ||
//创建SimpleMailMessage对象 | ||
SimpleMailMessage message = new SimpleMailMessage(); | ||
//邮件发送人 | ||
message.setFrom(Objects.requireNonNull(mailSender.getUsername())); | ||
//邮件接收人 | ||
message.setTo(stuId + "@stu.xidian.edu.cn"); | ||
|
||
//邮件主题 | ||
message.setSubject("【仙电搭子】注册账号验证码"); | ||
//邮件内容 | ||
message.setText("亲爱的用户:您正在进行邮箱验证,本次请求的验证码为:" + verCode + ",本验证码10分钟内有效,请在10分钟内完成验证。(请勿泄露此验证码)如非本人操作,请忽略该邮件。" + | ||
"(这是一封自动发送的邮件,请不要直接回复)"); | ||
//发送邮件 | ||
log.info("send message:" + message.toString()); | ||
mailSender.send(message); | ||
|
||
return true; | ||
} | ||
} |