diff --git a/.gitignore b/.gitignore
index f6197c0..449bd92 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,3 +35,5 @@ build/
/target/
application.yml
5963105_www.peteralbus.com.pfx
+SmsUtil.java
+SmsUtil
diff --git a/pom.xml b/pom.xml
index d3a10ab..5ea668c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,6 +25,11 @@
org.springframework.boot
spring-boot-starter-jdbc
+
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
org.springframework.boot
@@ -72,6 +77,18 @@
0.4.15
+
+
+ com.aliyun
+ aliyun-java-sdk-core
+ 4.6.0
+
+
+ com.aliyun
+ aliyun-java-sdk-dysmsapi
+ 2.2.1
+
+
org.springframework.boot
spring-boot-starter-test
diff --git a/src/main/java/com/peteralbus/controller/UserController.java b/src/main/java/com/peteralbus/controller/UserController.java
index 03d1c4c..2e3444c 100644
--- a/src/main/java/com/peteralbus/controller/UserController.java
+++ b/src/main/java/com/peteralbus/controller/UserController.java
@@ -2,13 +2,23 @@ package com.peteralbus.controller;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
+import com.aliyuncs.exceptions.ClientException;
import com.peteralbus.domain.User;
import com.peteralbus.service.UserService;
+import com.peteralbus.util.RandomUtil;
+import com.peteralbus.util.RedisUtils;
+import com.peteralbus.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mail.javamail.JavaMailSender;
+import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
+import javax.mail.internet.MimeMessage;
+import java.util.Objects;
+import java.util.concurrent.TimeUnit;
+
/**
* The type User controller.
* @author PeterAlbus
@@ -18,7 +28,15 @@ import org.springframework.web.bind.annotation.RestController;
@CrossOrigin
public class UserController
{
- UserService userService;
+ private UserService userService;
+ private RedisUtils redisUtils;
+ private JavaMailSender javaMailSender;
+
+ @Autowired
+ public void setRedisUtils(RedisUtils redisUtils)
+ {
+ this.redisUtils = redisUtils;
+ }
@Autowired
public void setUserService(UserService userService)
@@ -26,6 +44,12 @@ public class UserController
this.userService = userService;
}
+ @Autowired
+ public void setJavaMailSender(JavaMailSender javaMailSender)
+ {
+ this.javaMailSender = javaMailSender;
+ }
+
@RequestMapping("/mailLogin")
public SaTokenInfo doLogin(String userMail, String userPassword)
{
@@ -44,7 +68,10 @@ public class UserController
if(StpUtil.isLogin())
{
Long userId=Long.valueOf((String) StpUtil.getLoginId());
- return userService.getUserById(userId);
+ User user=userService.getUserById(userId);
+ user.setUserPassword(null);
+ user.setUserSalt(null);
+ return user;
}
return null;
}
@@ -56,10 +83,74 @@ public class UserController
return "logout";
}
+ @RequestMapping("/applyMailVerifyCode")
+ public String applyMailVerifyCode(String account)
+ {
+ String verifyCode=RandomUtil.generateVerifyCode(6);
+ try {
+ MimeMessage message=javaMailSender.createMimeMessage();
+ MimeMessageHelper helper=new MimeMessageHelper(message,true);
+ helper.setFrom("wuhongdb@163.com","PeterAlbus");
+ helper.setTo(account);
+ helper.setSubject("[验证码]PeterAlbus的博客注册");
+ helper.setText("您正在注册PeterAlbus的个人博客账号,验证码为:"+verifyCode+",有效期10分钟。若非本人操作,请忽略此邮件!",false);
+ javaMailSender.send(message);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return "sendMailFail";
+ }
+ redisUtils.set("verifyCode:"+account,verifyCode,10L, TimeUnit.MINUTES);
+ return "sendMailSuccess";
+ }
+
+ @RequestMapping("/applyPhoneVerifyCode")
+ public String applyPhoneVerifyCode(String account)
+ {
+ final String success ="smsSendSuccess";
+ String result="";
+ String verifyCode=RandomUtil.generateVerifyCode(4);
+ try
+ {
+ result=SmsUtil.sendSms(account,verifyCode);
+ if(Objects.equals(result, success))
+ {
+ redisUtils.set("verifyCode:"+account,verifyCode,10L, TimeUnit.MINUTES);
+ return "sendMailSuccess";
+ }
+ }
+ catch (ClientException e)
+ {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
@RequestMapping("/register")
- public String register(User user)
+ public String register(User user,String verifyCode)
{
- return userService.register(user);
+ String verifyCodeKey;
+ if(user.getUserMail()!=null)
+ {
+ verifyCodeKey="verifyCode:"+user.getUserMail();
+ }
+ else if(user.getUserPhone()!=null)
+ {
+ verifyCodeKey="verifyCode:"+user.getUserPhone();
+ }
+ else
+ {
+ return "needRequestVerifyCode";
+ }
+ if(redisUtils.exists(verifyCodeKey))
+ {
+ if(redisUtils.get(verifyCodeKey).equals(verifyCode))
+ {
+ redisUtils.remove(verifyCodeKey);
+ return userService.register(user);
+ }
+ return "wrongVerifyCode";
+ }
+ return "needRequestVerifyCode";
}
@RequestMapping("/changePassword")
diff --git a/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java b/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java
index 3f1e20b..f665cab 100644
--- a/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java
+++ b/src/main/java/com/peteralbus/service/impl/UserServiceImpl.java
@@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peteralbus.domain.User;
import com.peteralbus.mapper.UserMapper;
import com.peteralbus.service.UserService;
-import com.peteralbus.util.Md5Util;
+import com.peteralbus.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -34,7 +34,8 @@ public class UserServiceImpl implements UserService
public String register(User user)
{
user.setGmtCreate(LocalDateTime.now());
- user.setUserSalt(Md5Util.getSalt(8));
+ user.setUserIdentity(5);
+ user.setUserSalt(RandomUtil.getSalt(8));
user.setUserPassword(SaSecureUtil.md5BySalt(user.getUserPassword(), user.getUserSalt()));
try
{
@@ -47,7 +48,6 @@ public class UserServiceImpl implements UserService
{
Throwable cause = e.getCause();
if (cause instanceof SQLIntegrityConstraintViolationException) {
- String sqlState = ((SQLIntegrityConstraintViolationException) cause).getSQLState();
return "repeatAccount";
} else {
e.printStackTrace();
diff --git a/src/main/java/com/peteralbus/util/Md5Util.java b/src/main/java/com/peteralbus/util/RandomUtil.java
similarity index 65%
rename from src/main/java/com/peteralbus/util/Md5Util.java
rename to src/main/java/com/peteralbus/util/RandomUtil.java
index 7a5c2b7..9f6457c 100644
--- a/src/main/java/com/peteralbus/util/Md5Util.java
+++ b/src/main/java/com/peteralbus/util/RandomUtil.java
@@ -7,7 +7,7 @@ import java.util.Random;
* @author PeterAlbus
* Created on 2022/3/26.
*/
-public class Md5Util
+public class RandomUtil
{
/**
* Get salt string.
@@ -25,4 +25,15 @@ public class Md5Util
}
return sb.toString();
}
+
+ public static String generateVerifyCode(int n){
+ Random r = new Random();
+ StringBuilder stringBuffer =new StringBuilder();
+ for(int i = 0;i < n;i ++){
+ int ran1 = r.nextInt(10);
+ stringBuffer.append(ran1);
+ }
+ return stringBuffer.toString();
+ }
+
}
diff --git a/src/main/java/com/peteralbus/util/SmsUtil.java.example b/src/main/java/com/peteralbus/util/SmsUtil.java.example
new file mode 100644
index 0000000..0ccf5aa
--- /dev/null
+++ b/src/main/java/com/peteralbus/util/SmsUtil.java.example
@@ -0,0 +1,54 @@
+package com.peteralbus.util;
+
+import com.aliyuncs.DefaultAcsClient;
+import com.aliyuncs.IAcsClient;
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
+import com.aliyuncs.exceptions.ClientException;
+import com.aliyuncs.profile.DefaultProfile;
+import com.aliyuncs.profile.IClientProfile;
+
+/**
+ * The type Sms util.
+ * @author PeterAlbus
+ * Created on 2022/3/27.
+ */
+public class SmsUtil
+{
+ private static final String PRODUCT = "Dysmsapi";
+
+ private static final String DOMAIN = "dysmsapi.aliyuncs.com";
+
+ private static final String ACCESS_KEY_ID = "your access key id";
+ private static final String ACCESS_KEY_SECRET = "your access key secret";
+ private static final String OK = "OK";
+ /**
+ * Send sms int.
+ *
+ * @return the int
+ */
+ public static String sendSms(String phone,String code) throws ClientException
+ {
+ System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
+ System.setProperty("sun.net.client.defaultReadTimeout", "10000");
+
+ IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
+ DefaultProfile.addEndpoint("cn-shanghai", PRODUCT, DOMAIN);
+ IAcsClient acsClient = new DefaultAcsClient(profile);
+
+ SendSmsRequest request = new SendSmsRequest();
+ request.setPhoneNumbers(phone);
+ /* 必填:短信签名-可在短信控制台中找到 */
+ request.setSignName("阿里云短信测试");
+ /* 必填:短信模板code-可在短信控制台中找到 */
+ request.setTemplateCode("SMS_154950909");
+ request.setTemplateParam("{\"code\":\"" + code + "\"}");
+
+ SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
+ if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals(OK)){
+ return "smsSendSuccess";
+ }else {
+ return sendSmsResponse.getMessage();
+ }
+ }
+}