手机号验证
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -147,7 +147,11 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-mail</artifactId>
|
<artifactId>spring-boot-starter-mail</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--redis-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|||||||
@@ -75,6 +75,8 @@ public class SmsUtil {
|
|||||||
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
|
.setTemplateCode(templateCode != null ? templateCode : this.templateCode)
|
||||||
.setTemplateParam(templateParam);
|
.setTemplateParam(templateParam);
|
||||||
|
|
||||||
|
log.error("TemplateParam 实际值 = {}", templateParam);
|
||||||
|
|
||||||
SendSmsResponse response = client.sendSms(sendSmsRequest);
|
SendSmsResponse response = client.sendSms(sendSmsRequest);
|
||||||
|
|
||||||
if ("OK".equals(response.getBody().getCode())) {
|
if ("OK".equals(response.getBody().getCode())) {
|
||||||
@@ -145,6 +147,7 @@ public class SmsUtil {
|
|||||||
jsonBuilder.append("}");
|
jsonBuilder.append("}");
|
||||||
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
|
return sendSms(phoneNumber, templateCode, jsonBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class WebConfig implements WebMvcConfigurer {
|
|||||||
.addPathPatterns("/**")
|
.addPathPatterns("/**")
|
||||||
// 忽略的接口
|
// 忽略的接口
|
||||||
.excludePathPatterns(
|
.excludePathPatterns(
|
||||||
"/user/login"
|
"/user/login",
|
||||||
|
"/user/sendCode"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,11 +12,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
/**
|
import java.util.Map;
|
||||||
* 用户接口
|
|
||||||
*
|
|
||||||
* @author KilLze
|
|
||||||
*/
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@@ -79,4 +76,18 @@ public class UserController {
|
|||||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/sendCode")
|
||||||
|
public Result sendCode(@RequestBody Map<String, String> body) {
|
||||||
|
String phone = body.get("phone");
|
||||||
|
userService.sendSmsCode(phone);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "验证码发送成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/loginByCode")
|
||||||
|
public Result loginByCode(@RequestBody Map<String, String> body){
|
||||||
|
boolean ok = userService.verifyCode(body.get("phone"), body.get("code"));
|
||||||
|
return ok ? Result.success(ResultCode.SUCCESS, "登录成功") : Result.error(ResultCode.SYSTEM_ERROR, "登录失败");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,14 +8,14 @@ import java.util.List;
|
|||||||
|
|
||||||
public interface CommentsMapper {
|
public interface CommentsMapper {
|
||||||
// 添加评论
|
// 添加评论
|
||||||
@Insert("INSERT INTO comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
|
@Insert("INSERT INTO dating.comments(content, user_id, post_id, created_at) VALUES(#{content}, #{user_id}, #{post_id}, #{created_at})")
|
||||||
int addComment(Comments comments);
|
int addComment(Comments comments);
|
||||||
|
|
||||||
// 删除评论
|
// 删除评论
|
||||||
@Delete("DELETE FROM comments WHERE user_id = #{user_id}")
|
@Delete("DELETE FROM dating.comments WHERE user_id = #{user_id}")
|
||||||
int deleteComments(@Param("user_id") Long user_id);
|
int deleteComments(@Param("user_id") Long user_id);
|
||||||
|
|
||||||
// 根据动态ID查询评论列表
|
// 根据动态ID查询评论列表
|
||||||
@Select("SELECT * FROM comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
|
@Select("SELECT * FROM dating.comments WHERE post_id = #{post_id} ORDER BY created_at DESC")
|
||||||
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
|
List<Comments> getCommentsByPostId(@Param("post_id") Long post_id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,8 @@ public interface UserService {
|
|||||||
* @return 更新后的用户信息
|
* @return 更新后的用户信息
|
||||||
*/
|
*/
|
||||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||||
|
|
||||||
|
void sendSmsCode(String phone);
|
||||||
|
|
||||||
|
boolean verifyCode(String phone, String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.bao.dating.service.impl;
|
|||||||
import com.bao.dating.common.aliyun.AliOssUtil;
|
import com.bao.dating.common.aliyun.AliOssUtil;
|
||||||
import com.bao.dating.common.aliyun.GreenImageScan;
|
import com.bao.dating.common.aliyun.GreenImageScan;
|
||||||
import com.bao.dating.common.aliyun.GreenTextScan;
|
import com.bao.dating.common.aliyun.GreenTextScan;
|
||||||
|
import com.bao.dating.common.aliyun.SmsUtil;
|
||||||
import com.bao.dating.common.result.AliOssResult;
|
import com.bao.dating.common.result.AliOssResult;
|
||||||
import com.bao.dating.common.result.GreenAuditResult;
|
import com.bao.dating.common.result.GreenAuditResult;
|
||||||
import com.bao.dating.context.UserContext;
|
import com.bao.dating.context.UserContext;
|
||||||
@@ -13,11 +14,13 @@ import com.bao.dating.pojo.entity.User;
|
|||||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
|
import com.bao.dating.util.CodeUtil;
|
||||||
import com.bao.dating.util.FileUtil;
|
import com.bao.dating.util.FileUtil;
|
||||||
import com.bao.dating.util.JwtUtil;
|
import com.bao.dating.util.JwtUtil;
|
||||||
import com.bao.dating.util.MD5Util;
|
import com.bao.dating.util.MD5Util;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -27,6 +30,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户服务实现类
|
* 用户服务实现类
|
||||||
@@ -36,6 +40,10 @@ import java.util.UUID;
|
|||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private SmsUtil smsUtil;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private AliOssUtil ossUtil;
|
private AliOssUtil ossUtil;
|
||||||
|
|
||||||
@@ -48,6 +56,9 @@ public class UserServiceImpl implements UserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private StringRedisTemplate stringRedisTemplate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户登录
|
* 用户登录
|
||||||
*
|
*
|
||||||
@@ -275,4 +286,41 @@ public class UserServiceImpl implements UserService {
|
|||||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||||
return userInfoVO;
|
return userInfoVO;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 发送短信验证码
|
||||||
|
@Override
|
||||||
|
public void sendSmsCode(String phone) {
|
||||||
|
//防刷:60 秒内只能发一次
|
||||||
|
String key = "sms:code:" + phone;
|
||||||
|
Boolean exists = stringRedisTemplate.hasKey(key);
|
||||||
|
if (Boolean.TRUE.equals(exists)){
|
||||||
|
throw new RuntimeException("请勿频繁发送验证码");
|
||||||
|
}
|
||||||
|
// 生成验证码
|
||||||
|
String code = CodeUtil.generateCode();
|
||||||
|
// 发送短信
|
||||||
|
smsUtil.sendVerificationCode(phone, code);
|
||||||
|
//存 Redis(5分钟过期)
|
||||||
|
stringRedisTemplate.opsForValue()
|
||||||
|
.set(key,code, 5, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验验证码
|
||||||
|
@Override
|
||||||
|
public boolean verifyCode(String phone, String code) {
|
||||||
|
|
||||||
|
String key = "sms:code:" + phone;
|
||||||
|
String realCode = stringRedisTemplate.opsForValue().get(key);
|
||||||
|
//过期,未发送
|
||||||
|
if (realCode ==null){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
//不匹配
|
||||||
|
if (!realCode.equals(code)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// 校验成功,删除验证码(一次性)
|
||||||
|
stringRedisTemplate.delete(key);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/main/java/com/bao/dating/util/CodeUtil.java
Normal file
12
src/main/java/com/bao/dating/util/CodeUtil.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.bao.dating.util;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class CodeUtil {
|
||||||
|
// 生成6位数字验证码
|
||||||
|
public static String generateCode() {
|
||||||
|
Random random = new Random();
|
||||||
|
int code=100000+random.nextInt(900000);
|
||||||
|
return String.valueOf(code);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user