2
0

feat: 忘记密码

This commit is contained in:
caiyuchao
2025-02-13 11:22:16 +08:00
parent 076fa284aa
commit d0c9eb320a
20 changed files with 218 additions and 16 deletions

View File

@@ -62,7 +62,7 @@ alipay:
aliPayCertPath: \u652F\u4ED8\u5B9D\u516C\u94A5\u8BC1\u4E66
aliPayRootCertPath: \u652F\u4ED8\u5B9D\u6839\u8BC1\u4E66
serverUrl: https://openapi-sandbox.dl.alipaydev.com/gateway.do
domain: http://192.168.9.50
domain: http://192.168.6.222
testDomain: http://129.204.171.210:8085
wxpay:

View File

@@ -45,13 +45,13 @@ public class SysEmailController extends BaseController {
if (!mailProperties.getEnabled()) {
return R.fail(MessageUtils.message("user.email.not.enable"));
}
String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
String key = GlobalConstants.CAPTCHA_CODE_KEY + GlobalConstants.SYSTEM + email;
String code = RandomUtil.randomNumbers(4);
redisService.setCacheObject(key, code, Constants.MAIL_CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
try {
MailUtils.sendText(email, "Registration verification code", "Your verification code is: " + code + ", The validity period is " + Constants.MAIL_CAPTCHA_EXPIRATION + " minutes, please fill in as soon as possible.");
MailUtils.sendText(email, "Your WANFI verification code", "Hi\nYour verification code is:\n" + code + "\nThis code will expire in " + Constants.MAIL_CAPTCHA_EXPIRATION + " minutes.\nFor your security, don't share it with anyone.");
} catch (Exception e) {
log.error("Verification code sending exception => {}", e.getMessage());
log.error("email verification code send failed => {}", e.getMessage());
return R.fail(e.getMessage());
}
return R.ok();

View File

@@ -1,6 +1,6 @@
package org.wfc.system.controller;
import java.util.Arrays;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -10,8 +10,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.wfc.common.core.constant.GlobalConstants;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.exception.CaptchaException;
import org.wfc.common.core.exception.VerificationCodeException;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.file.FileTypeUtils;
@@ -20,13 +23,17 @@ import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.service.TokenService;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.RemoteFileService;
import org.wfc.system.api.domain.SysFile;
import org.wfc.system.api.domain.SysUser;
import org.wfc.system.domain.bo.SysUserForgotPwdBo;
import org.wfc.system.service.ISysUserService;
import java.util.Arrays;
/**
* 个人信息 业务处理
*
@@ -45,6 +52,9 @@ public class SysProfileController extends BaseController
@Autowired
private RemoteFileService remoteFileService;
@Autowired
private RedisService redisService;
/**
* 个人信息
*/
@@ -122,6 +132,51 @@ public class SysProfileController extends BaseController
}
return error(MessageUtils.message("system.modify.password.failed.contact.administrator"));
}
/**
* 忘记密码
*/
@PutMapping("/forgotPwd")
public AjaxResult forgotPwd(@RequestBody SysUserForgotPwdBo userForgotPwdBo)
{
SysUser user = userService.selectUserByEmail(userForgotPwdBo.getEmail());
if (ObjectUtil.isNull(user)) {
return error("user.register.email.not.exist.error");
}
checkCaptchaEmail(userForgotPwdBo.getCode(), userForgotPwdBo.getEmail());
String newPassword = userForgotPwdBo.getPassword();
String password = user.getPassword();
String username = user.getUserName();
if (SecurityUtils.matchesPassword(newPassword, password))
{
return error("user.modify.password.failed.new.password.same.old.password");
}
newPassword = SecurityUtils.encryptPassword(newPassword);
if (userService.resetUserPwd(username, newPassword) > 0)
{
return success();
}
return error("user.modify.password.failed.contact.administrator");
}
public void checkCaptchaEmail(String code, String email) throws CaptchaException
{
if (StringUtils.isEmpty(code))
{
throw new VerificationCodeException("email.code.not.blankk");
}
String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + GlobalConstants.SYSTEM + StringUtils.nvl(email, "");
String captcha = redisService.getCacheObject(verifyKey);
if (captcha == null)
{
throw new VerificationCodeException();
}
if (!code.equalsIgnoreCase(captcha))
{
throw new VerificationCodeException();
}
}
/**
* 头像上传

View File

@@ -0,0 +1,15 @@
package org.wfc.system.domain.bo;
import lombok.Data;
/**
* @description: 忘记密码bo
* @author: cyc
* @since: 2025-02-12
*/
@Data
public class SysUserForgotPwdBo {
private String email;
private String password;
private String code;
}

View File

@@ -44,6 +44,14 @@ public interface SysUserMapper
*/
public SysUser selectUserByUserName(String userName);
/**
* 通过邮箱查询用户
*
* @param email 邮箱
* @return 用户对象信息
*/
public SysUser selectUserByEmail(String email);
/**
* 通过用户ID查询用户
*

View File

@@ -1,8 +1,9 @@
package org.wfc.system.service;
import java.util.List;
import org.wfc.system.api.domain.SysUser;
import java.util.List;
/**
* 用户 业务层
*
@@ -42,6 +43,14 @@ public interface ISysUserService
*/
public SysUser selectUserByUserName(String userName);
/**
* 通过邮箱查询用户
*
* @param email 用户名
* @return 用户对象信息
*/
public SysUser selectUserByEmail(String email);
/**
* 通过用户ID查询用户
*

View File

@@ -1,9 +1,5 @@
package org.wfc.system.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Validator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +27,11 @@ import org.wfc.system.service.ISysConfigService;
import org.wfc.system.service.ISysDeptService;
import org.wfc.system.service.ISysUserService;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户 业务层处理
*
@@ -116,6 +117,11 @@ public class SysUserServiceImpl implements ISysUserService
return userMapper.selectUserByUserName(userName);
}
@Override
public SysUser selectUserByEmail(String email) {
return userMapper.selectUserByEmail(email);
}
/**
* 通过用户ID查询用户
*

View File

@@ -127,6 +127,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
</select>
<select id="selectUserByEmail" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.email = #{email} and u.del_flag = '0'
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
<include refid="selectUserVo"/>

View File

@@ -45,7 +45,7 @@ public class UEmailController extends BaseController {
if (!mailProperties.getEnabled()) {
return R.fail(MessageUtils.message("user.email.not.enable"));
}
String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
String key = GlobalConstants.CAPTCHA_CODE_KEY + GlobalConstants.USER + email;
String code = RandomUtil.randomNumbers(4);
redisService.setCacheObject(key, code, Constants.MAIL_CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
try {

View File

@@ -1,23 +1,33 @@
package org.wfc.user.controller;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.wfc.common.core.constant.GlobalConstants;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.exception.CaptchaException;
import org.wfc.common.core.exception.VerificationCodeException;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.file.FileTypeUtils;
import org.wfc.common.core.utils.file.MimeTypeUtils;
import org.wfc.common.core.web.controller.BaseController;
import org.wfc.common.core.web.domain.AjaxResult;
import org.wfc.common.log.annotation.Log;
import org.wfc.common.log.enums.BusinessType;
import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.service.TokenService;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.user.api.RemoteUFileService;
import org.wfc.user.api.domain.UFile;
import org.wfc.user.api.domain.UUser;
import org.wfc.user.domain.bo.UUserForgotPwdBo;
import org.wfc.user.service.IUUserService;
import java.util.Arrays;
@@ -40,6 +50,9 @@ public class UProfileController extends BaseController
@Autowired
private RemoteUFileService remoteUFileService;
@Autowired
private RedisService redisService;
/**
* 个人信息
*/
@@ -123,6 +136,44 @@ public class UProfileController extends BaseController
}
return error(MessageUtils.message("user.modify.password.failed.contact.administrator"));
}
/**
* 忘记密码
*/
@PutMapping("/forgotPwd")
public AjaxResult forgotPwd(@RequestBody UUserForgotPwdBo UUserForgotPwdBo) {
UUser user = userService.selectUserByEmail(UUserForgotPwdBo.getEmail());
if (ObjectUtil.isNull(user)) {
return error("user.register.email.not.exist.error");
}
checkCaptchaEmail(UUserForgotPwdBo.getCode(), UUserForgotPwdBo.getEmail());
String newPassword = UUserForgotPwdBo.getPassword();
String password = user.getPassword();
String username = user.getUserName();
if (SecurityUtils.matchesPassword(newPassword, password)) {
return error("user.modify.password.failed.new.password.same.old.password");
}
newPassword = SecurityUtils.encryptPassword(newPassword);
if (userService.resetUserPwd(username, newPassword) > 0) {
return success();
}
return error("user.modify.password.failed.contact.administrator");
}
public void checkCaptchaEmail(String code, String email) throws CaptchaException {
if (StringUtils.isEmpty(code)) {
throw new VerificationCodeException("email.code.not.blankk");
}
String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + GlobalConstants.USER + StringUtils.nvl(email, "");
String captcha = redisService.getCacheObject(verifyKey);
if (captcha == null) {
throw new VerificationCodeException();
}
if (!code.equalsIgnoreCase(captcha)) {
throw new VerificationCodeException();
}
}
/**
* 头像上传

View File

@@ -0,0 +1,15 @@
package org.wfc.user.domain.bo;
import lombok.Data;
/**
* @description: 忘记密码bo
* @author: cyc
* @since: 2025-02-12
*/
@Data
public class UUserForgotPwdBo {
private String email;
private String password;
private String code;
}

View File

@@ -44,6 +44,14 @@ public interface UUserMapper
*/
public UUser selectUserByUserName(String userName);
/**
* 通过邮箱查询用户
*
* @param email 邮箱
* @return 用户对象信息
*/
public UUser selectUserByEmail(String email);
/**
* 通过用户ID查询用户
*

View File

@@ -43,6 +43,14 @@ public interface IUUserService
*/
public UUser selectUserByUserName(String userName);
/**
* 通过邮箱查询用户
*
* @param email 用户名
* @return 用户对象信息
*/
public UUser selectUserByEmail(String email);
/**
* 通过用户ID查询用户
*

View File

@@ -109,6 +109,11 @@ public class UUserServiceImpl implements IUUserService
return userMapper.selectUserByUserName(userName);
}
@Override
public UUser selectUserByEmail(String email) {
return userMapper.selectUserByEmail(email);
}
/**
* 通过用户ID查询用户
*

View File

@@ -149,6 +149,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
</select>
<select id="selectUserByEmail" parameterType="String" resultMap="UUserResult">
<include refid="selectUserVo"/>
where u.email = #{email} and u.del_flag = '0'
</select>
<select id="selectUserById" parameterType="Long" resultMap="UUserResult">
<include refid="selectUserVo"/>