add 用户使用短信修改密码

This commit is contained in:
lijiarao
2021-09-24 10:24:48 +08:00
parent e6ed1ee736
commit 5683f6a54c
9 changed files with 108 additions and 78 deletions
@@ -3,7 +3,6 @@ package com.jero.company.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.api.vo.Result;
import com.jero.common.constant.CacheConstant;
import com.jero.common.constant.CommonConstant;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.api.ISysBaseAPI;
@@ -199,15 +198,15 @@ public class CompanyLoginController {
@ApiOperation(value = "发送手机号验证码")
@GetMapping(value = "/sendVerificationCode")
public Result<?> sendVerificationCode(String mobile) {
mobile = PasswordUtil.encrypt(mobile);
public Result<?> sendVerificationCode(String phone) {
phone = PasswordUtil.encrypt(phone);
LambdaQueryWrapper<PayContactsManagement> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(PayContactsManagement::getPhone, mobile);
queryWrapper.eq(PayContactsManagement::getPhone, phone);
PayContactsManagement contactsManagement = payContactsManagementService.getOne(queryWrapper);
if (Objects.isNull(contactsManagement)){
throw new JeroBootException("手机号不是企业联系人");
}
sysBaseAPI.sendVerificationCode(mobile,"2",null,null);
sysBaseAPI.sendVerificationCode(phone,"2",null,null);
if (StringUtils.isBlank(contactsManagement.getPassword())){
return Result.error("updatePassword");
}
@@ -215,9 +214,9 @@ public class CompanyLoginController {
}
@ApiOperation(value = "短信验证码修改密码")
@PostMapping(value = "/changePassword")
public Result<?> changePassword(@RequestBody CompanyLoginVO companyLoginVO) {
payContactsManagementService.changePassword(companyLoginVO);
@PostMapping(value = "/verificationChangePassword")
public Result<?> verificationChangePassword(@RequestBody CompanyLoginVO companyLoginVO) {
payContactsManagementService.verificationChangePassword(companyLoginVO);
return Result.OK("修改密码成功");
}
@@ -49,5 +49,5 @@ public interface IPayContactsManagementService extends IService<PayContactsManag
*/
List<PayContactsManagement> listPayContactsManagement(List<String> phoneList);
void changePassword(CompanyLoginVO companyLoginVO);
void verificationChangePassword(CompanyLoginVO companyLoginVO);
}
@@ -14,7 +14,6 @@ import com.jero.company.entity.PayContactsManagement;
import com.jero.company.mapper.PayContactsManagementMapper;
import com.jero.company.service.IPayContactsManagementService;
import com.jero.company.vo.CompanyLoginVO;
import com.jero.modules.system.entity.SysUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -64,14 +63,14 @@ public class PayContactsManagementServiceImpl extends ServiceImpl<PayContactsMan
}
@Override
public void changePassword(CompanyLoginVO companyLoginVO) {
public void verificationChangePassword(CompanyLoginVO companyLoginVO) {
ValidUtil.validate(companyLoginVO);
//前端传入密码解密
String phone = companyLoginVO.getPhone();
String password = companyLoginVO.getPassword();
String verifyPassword = companyLoginVO.getVerifyPassword();
String RSAPublicKey = companyLoginVO.getRsapublickey();
String RSAPublicKey = companyLoginVO.getRsaPublicKey();
String RSAPrivateKey = String.valueOf(redisUtil.get(RSAPublicKey));
try {
password = CommonUtils.decryptBtRsaPriKey(password, RSAPrivateKey);
@@ -47,6 +47,6 @@ public class CompanyLoginVO {
@ApiModelProperty(value = "RSA公钥")
@NotEmpty(message = "RSA公钥不能为空!")
private String rsapublickey;
private String rsaPublicKey;
}
@@ -5,6 +5,8 @@ import cn.hutool.crypto.asymmetric.RSA;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.system.model.SysLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
@@ -34,9 +36,6 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.*;
/**
@@ -594,10 +593,24 @@ public class LoginController {
return authenticationUtils.getPublicKey();
}
@ApiOperation(value = "忘记密码", notes = "忘记密码")
@PostMapping(value = "/forgetPassword")
public Result<?> forgetPassword(@RequestBody SysUser user) {
sysUserService.forgetPassword(user);
@ApiOperation(value = "短信验证码修改密码")
@PostMapping(value = "/verificationChangePassword")
public Result<?> verificationChangePassword(@RequestBody SysLoginVO loginVO) {
sysUserService.verificationChangePassword(loginVO);
return Result.OK("修改密码成功");
}
@ApiOperation(value = "发送手机号验证码")
@GetMapping(value = "/sendVerificationCode")
public Result<?> sendVerificationCode(String phone) {
phone = PasswordUtil.encrypt(phone);
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getPhone, phone);
SysUser sysUser = sysUserService.getOne(queryWrapper);
if (Objects.isNull(sysUser)){
throw new JeroBootException("手机号不存在");
}
sysBaseAPI.sendVerificationCode(phone,"2",null,null);
return Result.OK();
}
}
@@ -1,7 +1,12 @@
package com.jero.modules.system.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
/**
* 登录表单
@@ -9,6 +14,7 @@ import io.swagger.annotations.ApiModelProperty;
* @Author scott
* @since 2019-01-18
*/
@Data
@ApiModel(value="登录对象", description="登录对象")
public class SysLoginModel {
@ApiModelProperty(value = "账号")
@@ -21,44 +27,4 @@ public class SysLoginModel {
private String checkKey;
@ApiModelProperty(value = "RSA公钥")
private String rsaPublicKey;
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 getCaptcha() {
return captcha;
}
public void setCaptcha(String captcha) {
this.captcha = captcha;
}
public String getCheckKey() {
return checkKey;
}
public void setCheckKey(String checkKey) {
this.checkKey = checkKey;
}
public String getRsaPublicKey() {
return rsaPublicKey;
}
public void setRsaPublicKey(String rsaPublicKey) {
this.rsaPublicKey = rsaPublicKey;
}
}
@@ -0,0 +1,51 @@
package com.jero.modules.system.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
/**
* @author liJiaRao
* @date 2021-09-24 10:00
*/
@Data
public class SysLoginVO {
@ApiModelProperty(value = "用户名")
private String username;
/**手机号*/
@ApiModelProperty(value = "手机号")
@NotEmpty(message = "手机号不能为空!")
@Pattern(regexp = "^((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(17[0135678])|(18[0-9])|(19[8|9])|(16[6]))\\d{8}$",message = "手机号码格式错误")
private String phone;
/**
* 密码
*/
@ApiModelProperty(value = "密码")
@NotEmpty(message = "密码不能为空!")
private String password;
/**
* 确认密码
*/
@ApiModelProperty(value = "确认密码")
@NotEmpty(message = "确认密码不能为空!")
private String verifyPassword;
/**
* 验证码
*/
@ApiModelProperty(value = "验证码")
@NotEmpty(message = "验证码不能为空!")
private String verifyCode;
@ApiModelProperty(value = "RSA公钥")
@NotEmpty(message = "RSA公钥不能为空!")
private String rsaPublicKey;
}
@@ -14,6 +14,7 @@ import com.jero.common.system.vo.SysUserCacheInfo;
import com.jero.modules.system.entity.SysUser;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.modules.system.model.SysLoginVO;
import com.jero.modules.system.model.SysUserSysDepartModel;
import com.jero.modules.system.vo.SysUserVo;
import org.springframework.transaction.annotation.Transactional;
@@ -241,5 +242,5 @@ public interface ISysUserService extends IService<SysUser> {
String resetPassword(String username);
void forgetPassword(SysUser user);
void verificationChangePassword(SysLoginVO loginVO);
}
@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.exception.JeroBootException;
import com.jero.common.util.*;
import com.jero.modules.system.model.SysLoginVO;
import com.jero.modules.system.vo.SysUserVo;
import lombok.extern.slf4j.Slf4j;
import com.jero.common.api.vo.Result;
@@ -475,46 +476,46 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
@Override
public void forgetPassword(SysUser user) {
fPRuleCheck(user);
public void verificationChangePassword(SysLoginVO loginVO) {
fPRuleCheck(loginVO);
//前端传入密码解密
String username = user.getUsername();
String password = user.getPassword();
String verifyPassword = user.getVerifyPassword();
String RSAPublicKey = user.getRsapublickey();
String phone = loginVO.getPhone();
String password = loginVO.getPassword();
String verifyPassword = loginVO.getVerifyPassword();
String RSAPublicKey = loginVO.getRsaPublicKey();
String RSAPrivateKey = String.valueOf(redisUtil.get(RSAPublicKey));
try {
password = CommonUtils.decryptBtRsaPriKey(password, RSAPrivateKey);
verifyPassword = CommonUtils.decryptBtRsaPriKey(verifyPassword, RSAPrivateKey);
user.setPassword(password);
user.setVerifyPassword(verifyPassword);
loginVO.setPassword(password);
loginVO.setVerifyPassword(verifyPassword);
} catch (Exception e) {
e.printStackTrace();
}
//处理手机获取验证码的逻辑
Object obj = redisUtil.get("_verification:_phone:" + user.getPhone());
Object obj = redisUtil.get("_verification:_phone:" + phone);
if (obj == null) {
throw new JeroBootException("验证码失效,请重新发送");
}
String code = obj.toString();
if (!user.getVerifyCode().equals(code)) {
if (!loginVO.getVerifyCode().equals(code)) {
throw new JeroBootException("验证码错误!");
}
if (!password.equals(verifyPassword)) {
throw new JeroBootException("新密码和确认密码不一致,请重新输入");
}
LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUser::getUsername, username);
SysUser sysUser = userMapper.selectOne(wrapper);
LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SysUser::getPhone, phone);
SysUser sysUser = this.getOne(queryWrapper);
if (ObjectUtils.isEmpty(sysUser)) {
throw new JeroBootException("用户不存在");
}
String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), password, sysUser.getSalt());
user.setPassword(passwordEncode);
userMapper.forgetPassword(user);
redisUtil.del("_verification:_phone:" + user.getPhone());
sysUser.setPassword(passwordEncode);
userMapper.forgetPassword(sysUser);
redisUtil.del("_verification:_phone:" + phone);
List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
//SysRole sysRole = sysRoleMapper.selectOne(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleCode, roles.get(0)));
}
@@ -523,7 +524,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
* 忘记密码校验
* @param user
*/
private void fPRuleCheck(SysUser user){
private void fPRuleCheck(SysLoginVO user){
if(ObjectUtils.isEmpty(user.getUsername())){
throw new JeroBootException("用户名为必填");
}else if(user.getUsername().length()>50){