【新增】忘记密码接口
This commit is contained in:
@@ -24,8 +24,8 @@ public class SaTokenConfig implements WebMvcConfigurer {
|
||||
registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
|
||||
// 拦截所有路由
|
||||
.addPathPatterns("/**")
|
||||
// 排除登录、注册、发送验证码接口
|
||||
.excludePathPatterns("/user/login", "/user/register", "/user/sendCode")
|
||||
// 排除登录、注册、发送验证码, 忘记密码接口
|
||||
.excludePathPatterns("/user/login", "/user/register", "/user/sendCode", "/user/forgetPassword")
|
||||
// 排除后台管理登录接口
|
||||
.excludePathPatterns("/sys/user/login")
|
||||
// 排除反馈接口(支持匿名提交)
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.corewing.app.dto.api;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
@Data
|
||||
public class ForgetPasswordRequest {
|
||||
|
||||
@NotBlank(message = "手机号或邮箱不能为空")
|
||||
private String account;
|
||||
|
||||
@NotBlank(message = "验证码不能为空")
|
||||
private String verificationCode;
|
||||
|
||||
@NotBlank(message = "密码不能为空")
|
||||
private String password;
|
||||
|
||||
}
|
||||
@@ -2,15 +2,14 @@ package com.corewing.app.modules.app;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.corewing.app.common.Result;
|
||||
import com.corewing.app.dto.api.LoginRequest;
|
||||
import com.corewing.app.dto.api.RegisterRequest;
|
||||
import com.corewing.app.dto.api.SendCodeRequest;
|
||||
import com.corewing.app.dto.api.UpdatePasswordRequest;
|
||||
import com.corewing.app.dto.api.*;
|
||||
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
|
||||
import com.corewing.app.entity.User;
|
||||
import com.corewing.app.service.UserService;
|
||||
import com.corewing.app.service.VerifyCodeService;
|
||||
import com.corewing.app.util.I18nUtil;
|
||||
import com.corewing.app.util.IpUtil;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
@@ -174,4 +173,14 @@ public class AppUserController {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 忘记密码
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
@PutMapping("/forgetPassword")
|
||||
public Result<String> forgetPassword(@RequestBody ForgetPasswordRequest request) {
|
||||
return Result.isBool(userService.forgetPassword(request));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,11 @@ package com.corewing.app.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.corewing.app.dto.biz.user.BizUserIdRequest;
|
||||
import com.corewing.app.dto.api.ForgetPasswordRequest;
|
||||
import com.corewing.app.dto.biz.user.BizUserStatusRequest;
|
||||
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
|
||||
import com.corewing.app.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用用户 Service 接口
|
||||
@@ -115,4 +114,11 @@ public interface UserService extends IService<User> {
|
||||
* @return
|
||||
*/
|
||||
boolean batchStatus(BizUserStatusRequest bizUserStatusRequest);
|
||||
|
||||
/**
|
||||
* 忘记密码
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
boolean forgetPassword(ForgetPasswordRequest request);
|
||||
}
|
||||
|
||||
@@ -3,11 +3,10 @@ package com.corewing.app.service.impl;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.corewing.app.common.page.PageContext;
|
||||
import com.corewing.app.dto.biz.user.BizUserIdRequest;
|
||||
import com.corewing.app.dto.api.ForgetPasswordRequest;
|
||||
import com.corewing.app.dto.biz.user.BizUserStatusRequest;
|
||||
import com.corewing.app.dto.biz.user.ResetPasswordRequest;
|
||||
import com.corewing.app.entity.User;
|
||||
@@ -16,14 +15,13 @@ import com.corewing.app.service.UserService;
|
||||
import com.corewing.app.service.VerifyCodeService;
|
||||
import com.corewing.app.util.I18nUtil;
|
||||
import com.corewing.app.util.Ip2RegionUtil;
|
||||
import com.corewing.app.util.RedisUtil;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 应用用户 Service 实现类
|
||||
@@ -34,11 +32,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
private final VerifyCodeService verifyCodeService;
|
||||
private final Ip2RegionUtil ip2RegionUtil;
|
||||
private final UserMapper userMapper;
|
||||
private final RedisUtil redisUtil;
|
||||
|
||||
public UserServiceImpl(VerifyCodeService verifyCodeService, Ip2RegionUtil ip2RegionUtil, UserMapper userMapper) {
|
||||
public UserServiceImpl(VerifyCodeService verifyCodeService, Ip2RegionUtil ip2RegionUtil, UserMapper userMapper, RedisUtil redisUtil) {
|
||||
this.verifyCodeService = verifyCodeService;
|
||||
this.ip2RegionUtil = ip2RegionUtil;
|
||||
this.userMapper = userMapper;
|
||||
this.redisUtil = redisUtil;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -255,4 +255,22 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean forgetPassword(ForgetPasswordRequest request) {
|
||||
User user = getByAccount(request.getAccount());
|
||||
if(user == null) {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.user.not.found"));
|
||||
}
|
||||
|
||||
String codeKey = String.format("verify_code:%s:%s", "forget", request.getAccount());
|
||||
String checkCode = redisUtil.get(codeKey).toString();
|
||||
if(!checkCode.equalsIgnoreCase(request.getVerificationCode())) {
|
||||
throw new RuntimeException(I18nUtil.getMessage("error.verify.code.invalid"));
|
||||
}
|
||||
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex(request.getPassword().getBytes(StandardCharsets.UTF_8));
|
||||
user.setPassword(encryptPassword);
|
||||
return updateById(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user