用户注册新增手机号验证码校验
parent
7986e31882
commit
12638f2f6d
|
@ -57,7 +57,12 @@
|
|||
<groupId>com.muyu</groupId>
|
||||
<artifactId>cloud-common-api-doc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 阿里大鱼 -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -11,10 +11,7 @@ import com.muyu.common.security.service.TokenService;
|
|||
import com.muyu.common.security.utils.SecurityUtils;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
|
@ -66,7 +63,13 @@ public class TokenController {
|
|||
@PostMapping("register")
|
||||
public Result<?> register (@RequestBody RegisterBody registerBody) {
|
||||
// 用户注册
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword(),registerBody.getPhonenumber());
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/getPhoneCode/{phonenumber}")
|
||||
public Result getPhoneCode(@PathVariable("phonenumber") String phonenumber) {
|
||||
return sysLoginService.getPhoneCode(phonenumber);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,54 @@ package com.muyu.auth.form;
|
|||
*
|
||||
* @author muyu
|
||||
*/
|
||||
public class RegisterBody extends LoginBody {
|
||||
public class RegisterBody {
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
*用户手机号
|
||||
*/
|
||||
private String phonenumber;
|
||||
|
||||
private String phoneCode;
|
||||
|
||||
public String getUsername () {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername (String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword () {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword (String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getPhonenumber() {
|
||||
return phonenumber;
|
||||
}
|
||||
|
||||
public void setPhonenumber(String phonenumber) {
|
||||
this.phonenumber = phonenumber;
|
||||
}
|
||||
|
||||
public String getPhoneCode() {
|
||||
return phoneCode;
|
||||
}
|
||||
|
||||
public void setPhoneCode(String phoneCode) {
|
||||
this.phoneCode = phoneCode;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package com.muyu.auth.service;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.muyu.auth.form.RegisterBody;
|
||||
import com.muyu.auth.util.TelSmsUtils;
|
||||
import com.muyu.common.core.constant.CacheConstants;
|
||||
import com.muyu.common.core.constant.Constants;
|
||||
import com.muyu.common.core.constant.SecurityConstants;
|
||||
|
@ -16,8 +19,15 @@ import com.muyu.common.system.remote.RemoteUserService;
|
|||
import com.muyu.common.system.domain.SysUser;
|
||||
import com.muyu.common.system.domain.LoginUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 登录校验方法
|
||||
*
|
||||
|
@ -37,6 +47,11 @@ public class SysLoginService {
|
|||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String,String>redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*/
|
||||
|
@ -98,10 +113,10 @@ public class SysLoginService {
|
|||
/**
|
||||
* 注册
|
||||
*/
|
||||
public void register (String username, String password) {
|
||||
public void register (String username, String password,String phonenumber) {
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
if (StringUtils.isAnyBlank(username,password,phonenumber)) {
|
||||
throw new ServiceException("用户/密码/手机号必须填写");
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
|
@ -111,12 +126,17 @@ public class SysLoginService {
|
|||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
throw new ServiceException("密码长度必须在5到20个字符之间");
|
||||
}
|
||||
if (phonenumber.length()<UserConstants.PHONE_MIN_LENGTH){
|
||||
throw new ServiceException("手机号长度必须在11个字符");
|
||||
}
|
||||
|
||||
// 注册用户信息
|
||||
SysUser sysUser = new SysUser();
|
||||
sysUser.setUserName(username);
|
||||
sysUser.setNickName(username);
|
||||
sysUser.setPassword(SecurityUtils.encryptPassword(password));
|
||||
sysUser.setPhonenumber(phonenumber);
|
||||
|
||||
Result<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
|
||||
|
||||
if (Result.FAIL == registerResult.getCode()) {
|
||||
|
@ -124,4 +144,44 @@ public class SysLoginService {
|
|||
}
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
}
|
||||
|
||||
public Result getPhoneCode(String phonenumber) {
|
||||
|
||||
|
||||
|
||||
// if (StringUtils.isBlank(phonenumber) || !StringUtils.isMatch(phonenumber, "^\\d{11}$")) {
|
||||
// return Result.error("手机号为空或无效");
|
||||
// }
|
||||
|
||||
// String cachedPhonenumber = redisTemplate.opsForValue().get("phonenumber");
|
||||
//
|
||||
// if (cachedPhonenumber == null || cachedPhonenumber.isEmpty()) {
|
||||
//
|
||||
// return Result.error("手机号为空");
|
||||
// }
|
||||
|
||||
//
|
||||
// if (redisService.hasKey(phonenumber.length()==11?phonenumber:"0"+phonenumber)){
|
||||
// return Result.error("请勿频繁发送验证码");
|
||||
// }
|
||||
|
||||
if (StringUtils.isBlank(phonenumber)){
|
||||
return Result.error("手机号不能为空");
|
||||
}
|
||||
|
||||
|
||||
String s = RandomUtil.randomNumbers(4);
|
||||
|
||||
|
||||
|
||||
redisTemplate.opsForValue().set(phonenumber,phonenumber,1,TimeUnit.DAYS);
|
||||
|
||||
TelSmsUtils.sendSms(phonenumber, new HashMap<String, String>() {{
|
||||
put("实训12A项目注册验证码","你正在注册四组服务,您的验证码是"+s);
|
||||
}});
|
||||
|
||||
|
||||
return Result.success("发送成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
package com.muyu.auth.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信工具类
|
||||
*/
|
||||
@Log4j2
|
||||
public class TelSmsUtils {
|
||||
|
||||
/**
|
||||
* 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限
|
||||
*/
|
||||
private static String accessKeyId = "LTAI5tDbRqXkC5i3SMrCSDcX";
|
||||
|
||||
private static String accessKeySecret = "XUzMZoHPLsjNLafHsdQnMElBWZATsu";
|
||||
|
||||
/**
|
||||
* 短信访问域名
|
||||
*/
|
||||
private static String endpoint = "dysmsapi.aliyuncs.com";
|
||||
/**
|
||||
* 短信签名
|
||||
*/
|
||||
private static String signName = "乐优购";
|
||||
|
||||
private static String templateCode = "SMS_163851467";
|
||||
|
||||
/**
|
||||
* 实例化短信对象
|
||||
*/
|
||||
private static Client client;
|
||||
|
||||
static {
|
||||
log.info("初始化短信服务开始");
|
||||
long startTime = System.currentTimeMillis();
|
||||
try {
|
||||
client = initClient();
|
||||
log.info("初始化短信成功:{}", signName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
log.info("初始化短信服务结束:耗时:{}MS", (System.currentTimeMillis() - startTime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化短信对象
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
private static Client initClient() throws Exception {
|
||||
Config config = new Config()
|
||||
// 您的AccessKey ID
|
||||
.setAccessKeyId(accessKeyId)
|
||||
// 您的AccessKey Secret
|
||||
.setAccessKeySecret(accessKeySecret);
|
||||
// 访问的域名
|
||||
config.endpoint = endpoint;
|
||||
return new Client(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送单条短信
|
||||
*
|
||||
* @param tel
|
||||
*/
|
||||
public static SendSmsResponseBody sendSms(String tel, Map<String, String> sendDataMap) {
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setPhoneNumbers(tel)
|
||||
.setSignName(signName)
|
||||
.setTemplateCode(templateCode)
|
||||
.setTemplateParam(JSONObject.toJSONString(sendDataMap));
|
||||
SendSmsResponse sendSmsResponse = null;
|
||||
try {
|
||||
log.info("发送短信验证码:消息内容是:【{}】", JSONObject.toJSONString(sendDataMap));
|
||||
sendSmsResponse = client.sendSms(sendSmsRequest);
|
||||
} catch (Exception e) {
|
||||
log.error("短信发送异常,手机号:【{}】,短信内容:【{}】,异常信息:【{}】", tel, sendDataMap, e);
|
||||
}
|
||||
return sendSmsResponse.getBody();
|
||||
}
|
||||
|
||||
}
|
|
@ -110,4 +110,9 @@ public class UserConstants {
|
|||
public static final int PASSWORD_MIN_LENGTH = 5;
|
||||
|
||||
public static final int PASSWORD_MAX_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* 手机号长度限制
|
||||
*/
|
||||
public static final int PHONE_MIN_LENGTH = 11;
|
||||
}
|
||||
|
|
|
@ -13,7 +13,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
|
|||
/**
|
||||
* 不需要拦截地址
|
||||
*/
|
||||
public static final String[] excludeUrls = {"/login", "/logout", "/refresh"};
|
||||
public static final String[] excludeUrls = {"/login", "/logout", "/refresh","/getPhoneCode/\\\\d{11}$"};
|
||||
|
||||
@Override
|
||||
public void addInterceptors (InterceptorRegistry registry) {
|
||||
|
|
|
@ -135,6 +135,8 @@ public class SysUser extends BaseEntity {
|
|||
*/
|
||||
private Long roleId;
|
||||
|
||||
|
||||
|
||||
public SysUser (Long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
@ -170,4 +172,6 @@ public class SysUser extends BaseEntity {
|
|||
public String getPhonenumber () {
|
||||
return phonenumber;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue