提交 af92ebe1 编写于 作者: rushui.chen's avatar rushui.chen

20190820 修改密码与忘记密码

上级 15804707
流水线 #13513 已失败 于阶段
......@@ -26,7 +26,6 @@ public class AccountUserInfoController extends AccountBaseController {
@Autowired
private AccountUserInfoService accountUserInfoService;
/**
* 修改用户信息接口
*
......
......@@ -35,7 +35,6 @@ public class ModifyMobileController extends AccountBaseController {
@Autowired
private ModifyMobileService modifyMobileService;
/**
* 修改手机号
*
......
package com.pica.cloud.account.account.server.controller;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.server.entity.AccountContact;
import com.pica.cloud.account.account.server.entity.AccountUser;
import com.pica.cloud.account.account.server.req.ModifyPasswordReq;
import com.pica.cloud.account.account.server.mapper.AccountContactMapper;
import com.pica.cloud.account.account.server.req.ForgetPasswordReq;
import com.pica.cloud.account.account.server.req.PasswordReq;
import com.pica.cloud.account.account.server.service.AccountContactServer;
import com.pica.cloud.account.account.server.service.PasswordService;
import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.account.account.server.util.RSAUtils;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResponse;
import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.redis.CacheClient;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
......@@ -21,9 +29,23 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/password")
public class PasswordController extends AccountBaseController {
@Autowired
private PasswordService passwordService;
@Autowired
private AccountContactServer accountContactServer;
@Autowired
private CacheClient cacheClient;
/**
* 修改密码
*
* @param params params
* @return PicaResponse
* @throws Exception
*/
@ApiOperation(value = "修改密码")
@PostMapping(value = "/modify")
public PicaResponse modifyPassword(@RequestBody String params) throws Exception {
......@@ -31,7 +53,7 @@ public class PasswordController extends AccountBaseController {
String token = accountUser.getToken();
if (StringUtils.isNotEmpty(token)) {
String json = RSAUtils.decrypt(params, RSAUtils.getPrivateKey());
ModifyPasswordReq modifyPasswordReq = JSONObject.parseObject(json, ModifyPasswordReq.class);
PasswordReq modifyPasswordReq = JSONObject.parseObject(json, PasswordReq.class);
modifyPasswordReq.setId(accountUser.getAcctId());
passwordService.modifyPassword(modifyPasswordReq);
return PicaResponse.toResponse("密码修改成功");
......@@ -39,4 +61,63 @@ public class PasswordController extends AccountBaseController {
throw new PicaException("请登陆");
}
}
/**
* 忘记密码
*
* @param params params
* @return PicaResponse
* @throws Exception
*/
@ApiOperation(value = "忘记密码操作")
@PostMapping(value = "/reset")
public PicaResponse forwardPassword(@RequestBody String params) throws Exception {
//TODO: 2019/8/20 当前手机号是否注册过,如果没有注册,请先注册
String json = RSAUtils.decrypt(params, RSAUtils.getPrivateKey());
ForgetPasswordReq forwardPasswordReq = JSONObject.parseObject(json, ForgetPasswordReq.class);
forwardPasswordReq.setFlag("4");
//是否注册校验
checkRegisterMobile(forwardPasswordReq.getMobile());
//手机号校验
AccountUtils.checkMobilePhone(forwardPasswordReq.getMobile());
//验证码校验
this.checkAuthCode(forwardPasswordReq);
passwordService.forgetPassword(forwardPasswordReq);
return PicaResponse.toResponse("密码修改成功");
}
/**
* 校验手机号是否注册过
*
* @param mobile
*/
private void checkRegisterMobile(String mobile) {
String encrypt = EncryptCreateUtil.encrypt(mobile);
AccountContact accountContact = accountContactServer.selectByMobile(encrypt);
if (accountContact == null) {
throw new PicaException("请先注册");
}
}
/**
* 校验验证码
*
* @param req
*/
private void checkAuthCode(ForgetPasswordReq req) {
String flag = StringUtils.isBlank(req.getFlag()) ? "0" : req.getFlag();
if (StringUtils.isBlank(req.getAuthCode())) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
String authCodeKey = AccountUtils.getAuthCodeKey(req.getMobile(), flag);
String cacheCode = cacheClient.get(authCodeKey); //从redis获取验证码
if (StringUtils.isBlank(cacheCode)) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "短信验证码已过期,请重新获取");
}
if (!StringUtils.equals(req.getAuthCode(), cacheCode)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
cacheClient.del(authCodeKey); //清除验证码
}
}
......@@ -3,6 +3,8 @@ package com.pica.cloud.account.account.server.entity;
import java.util.Date;
public class LogPasswordModify {
private Long id;
private String mobilePhone;
......@@ -21,6 +23,12 @@ public class LogPasswordModify {
private Date createdTime;
public LogPasswordModify() {
this.modifiedTime = new Date();
this.deleteFlag = 1;
this.createdTime = new Date();
}
public Long getId() {
return id;
}
......@@ -33,7 +41,7 @@ public class LogPasswordModify {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone ) {
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone == null ? null : mobilePhone.trim();
}
......
......@@ -13,5 +13,22 @@ public interface AccountContactMapper {
*/
int updateByPrimaryKeySelective(AccountContact record);
/**
* 获取账户信息
*
* @param id
* @return
*/
AccountContact selectByPrimaryKey(Long id);
/**
* 通过手机号获取账户信息
*
* @param mobile
* @return
*/
AccountContact selectByMobile(String mobile);
}
\ No newline at end of file
......@@ -23,4 +23,5 @@ public interface AccountDetailsMapper {
* @return
*/
int updatePasswordById(AccountInfo record);
}
\ No newline at end of file
package com.pica.cloud.account.account.server.req;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(value = "忘记密码")
public class ForgetPasswordReq {
@ApiModelProperty(value = "账户id")
private Integer acctId;
@ApiModelProperty(value = "手机号")
private String mobile;
@ApiModelProperty(value = "验证码")
private String authCode;
@ApiModelProperty(value = "新密码")
private String newPsw;
@ApiModelProperty("验证码类型")
private String flag;
public Integer getAcctId() {
return acctId;
}
public void setAcctId(Integer acctId) {
this.acctId = acctId;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getAuthCode() {
return authCode;
}
public void setAuthCode(String authCode) {
this.authCode = authCode;
}
public String getNewPsw() {
return newPsw;
}
public void setNewPsw(String newPsw) {
this.newPsw = newPsw;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
......@@ -13,7 +13,6 @@ public class ModifyMobileReq {
@ApiModelProperty(value = "验证码类型")
private String flag;
public String getMobile() {
return mobile;
}
......
......@@ -4,7 +4,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel(description = "修改密码")
public class ModifyPasswordReq {
public class PasswordReq {
@ApiModelProperty("账户id")
private int id;
......
package com.pica.cloud.account.account.server.service;
import com.pica.cloud.account.account.server.entity.AccountContact;
public interface AccountContactServer {
AccountContact selectByMobile(String mobile);
}
package com.pica.cloud.account.account.server.service;
import com.pica.cloud.account.account.server.req.ModifyPasswordReq;
import com.pica.cloud.account.account.server.req.ForgetPasswordReq;
import com.pica.cloud.account.account.server.req.PasswordReq;
public interface PasswordService {
......@@ -11,5 +12,8 @@ public interface PasswordService {
* @param modifyPasswordReq
* @return
*/
void modifyPassword(ModifyPasswordReq modifyPasswordReq);
void modifyPassword(PasswordReq modifyPasswordReq);
void forgetPassword(ForgetPasswordReq forgetPasswordReq);
}
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.server.entity.AccountContact;
import com.pica.cloud.account.account.server.mapper.AccountContactMapper;
import com.pica.cloud.account.account.server.service.AccountContactServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class AccountContactServerImpl implements AccountContactServer {
@Autowired
private AccountContactMapper accountContactMapper;
@Override
public AccountContact selectByMobile(String mobile) {
return accountContactMapper.selectByMobile(mobile);
}
}
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.server.entity.AccountContact;
import com.pica.cloud.account.account.server.entity.AccountInfo;
import com.pica.cloud.account.account.server.entity.LogPasswordModify;
import com.pica.cloud.account.account.server.mapper.AccountContactMapper;
import com.pica.cloud.account.account.server.mapper.AccountDetailsMapper;
import com.pica.cloud.account.account.server.req.ModifyPasswordReq;
import com.pica.cloud.account.account.server.mapper.LogPasswordModifyMapper;
import com.pica.cloud.account.account.server.req.ForgetPasswordReq;
import com.pica.cloud.account.account.server.req.PasswordReq;
import com.pica.cloud.account.account.server.service.PasswordService;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -15,16 +19,47 @@ public class PasswordServiceImpl implements PasswordService {
@Autowired
private AccountDetailsMapper accountDetailsMapper;
@Autowired
private LogPasswordModifyMapper logPasswordModifyMapper;
@Autowired
private AccountContactMapper accountContactMapper;
@Override
public void modifyPassword(ModifyPasswordReq modifyPasswordReq) {
public void modifyPassword(PasswordReq modifyPasswordReq) {
Integer id = modifyPasswordReq.getId();
AccountInfo AccountInfoEntity = accountDetailsMapper.selectByPrimaryKey(id.longValue());
//更新密码
AccountInfo accountInfo = new AccountInfo();
accountInfo.setId(id.longValue());
accountInfo.setPassword(modifyPasswordReq.getOldPsw());
accountDetailsMapper.updatePasswordById(accountInfo);
//记录密码更新日志
// TODO: 2019/8/20
LogPasswordModify logPasswordModify = new LogPasswordModify();
logPasswordModify.setCreatedId(id.longValue());
logPasswordModify.setModifiedId(id);
logPasswordModify.setNewPwd(modifyPasswordReq.getNewPsw());
logPasswordModify.setOldPwd(AccountInfoEntity.getPassword());
AccountContact accountContact = accountContactMapper.selectByPrimaryKey(id.longValue());
logPasswordModify.setMobilePhone(accountContact.getMobilePhone());
logPasswordModifyMapper.insertSelective(logPasswordModify);
}
@Override
public void forgetPassword(ForgetPasswordReq forgetPasswordReq) {
AccountContact accountContact = accountContactMapper.selectByMobile(forgetPasswordReq.getMobile());
AccountInfo accountInfo = new AccountInfo();
accountInfo.setId(accountContact.getAcctId().longValue());
accountInfo.setPassword(forgetPasswordReq.getNewPsw());
accountDetailsMapper.updatePasswordById(accountInfo);
//记录更新日志
LogPasswordModify logPasswordModify = new LogPasswordModify();
logPasswordModify.setCreatedId(accountContact.getAcctId().longValue());
logPasswordModify.setModifiedId(accountContact.getAcctId());
logPasswordModify.setNewPwd(forgetPasswordReq.getNewPsw());
logPasswordModify.setOldPwd("");
logPasswordModify.setMobilePhone(forgetPasswordReq.getMobile());
logPasswordModifyMapper.insertSelective(logPasswordModify);
}
}
package com.pica.cloud.account.account.server.util;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import com.pica.cloud.foundation.utils.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;
/**
* 账户工具类
*/
public class AccountUtils {
private static final String AUTH_CODE_PREFIX = "authCode-";
//手机格式校验
public static void checkMobilePhone(String mobilePhone) {
if (StringUtils.isBlank(mobilePhone) || !ValidateUtils.isMobile(mobilePhone)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "请输入正确的手机号");
}
}
//获取验证码redis key
public static String getAuthCodeKey(String mobilePhone, String flag) {
return AUTH_CODE_PREFIX + flag + "-" + EncryptCreateUtil.encrypt(mobilePhone);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pica.cloud.account.account.server.mapper.AccountContactMapper" >
<resultMap id="BaseResultMap" type="com.pica.cloud.account.account.server.entity.AccountContact" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="acct_id" property="acctId" jdbcType="INTEGER" />
<result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR" />
<result column="delete_flag" property="deleteFlag" jdbcType="INTEGER" />
<result column="created_id" property="createdId" jdbcType="BIGINT" />
<result column="created_time" property="createdTime" jdbcType="TIMESTAMP" />
<result column="modified_id" property="modifiedId" jdbcType="BIGINT" />
<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP" />
</resultMap>
<sql id="Base_Column_List" >
<mapper namespace="com.pica.cloud.account.account.server.mapper.AccountContactMapper">
<resultMap id="BaseResultMap" type="com.pica.cloud.account.account.server.entity.AccountContact">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="acct_id" property="acctId" jdbcType="INTEGER"/>
<result column="mobile_phone" property="mobilePhone" jdbcType="VARCHAR"/>
<result column="delete_flag" property="deleteFlag" jdbcType="INTEGER"/>
<result column="created_id" property="createdId" jdbcType="BIGINT"/>
<result column="created_time" property="createdTime" jdbcType="TIMESTAMP"/>
<result column="modified_id" property="modifiedId" jdbcType="BIGINT"/>
<result column="modified_time" property="modifiedTime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
id, acct_id, mobile_phone, delete_flag, created_id, created_time, modified_id, modified_time
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
select
<include refid="Base_Column_List" />
from acct_contact
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >
delete from acct_contact
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.pica.cloud.account.account.server.entity.AccountContact" >
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List"/>
from acct_contact
where id = #{id,jdbcType=BIGINT}
</select>
<!--通过手机号获取账户信息-->
<select id="selectByMobile" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List"/>
from acct_contact
where mobile = #{mobile_phone}
</select>
<!--修改手机号-->
<update id="updateByPrimaryKeySelective"
parameterType="com.pica.cloud.account.account.server.entity.AccountContact">
update acct_contact
<set>
<if test="mobilePhone != null">
mobile_phone = #{mobilePhone,jdbcType=VARCHAR},
</if>
<if test="deleteFlag != null">
delete_flag = #{deleteFlag,jdbcType=INTEGER},
</if>
<if test="createdId != null">
created_id = #{createdId,jdbcType=BIGINT},
</if>
<if test="createdTime != null">
created_time = #{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedId != null">
modified_id = #{modifiedId,jdbcType=BIGINT},
</if>
<if test="modifiedTime != null">
modified_time = #{modifiedTime,jdbcType=TIMESTAMP},
</if>
</set>
where acct_id = #{acctId,jdbcType=BIGINT}
</update>
<insert id="insert" parameterType="com.pica.cloud.account.account.server.entity.AccountContact">
insert into acct_contact (id, acct_id, mobile_phone,
delete_flag, created_id, created_time,
modified_id, modified_time)
......@@ -32,86 +67,61 @@
#{deleteFlag,jdbcType=INTEGER}, #{createdId,jdbcType=BIGINT}, #{createdTime,jdbcType=TIMESTAMP},
#{modifiedId,jdbcType=BIGINT}, #{modifiedTime,jdbcType=TIMESTAMP})
</insert>
<insert id="insertSelective" parameterType="com.pica.cloud.account.account.server.entity.AccountContact" >
insert into acct_contact
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="acctId != null" >
acct_id,
</if>
<if test="mobilePhone != null" >
mobile_phone,
</if>
<if test="deleteFlag != null" >
delete_flag,
</if>
<if test="createdId != null" >
created_id,
</if>
<if test="createdTime != null" >
created_time,
</if>
<if test="modifiedId != null" >
modified_id,
</if>
<if test="modifiedTime != null" >
modified_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=BIGINT},
</if>
<if test="acctId != null" >
#{acctId,jdbcType=INTEGER},
</if>
<if test="mobilePhone != null" >
#{mobilePhone,jdbcType=VARCHAR},
</if>
<if test="deleteFlag != null" >
#{deleteFlag,jdbcType=INTEGER},
</if>
<if test="createdId != null" >
#{createdId,jdbcType=BIGINT},
</if>
<if test="createdTime != null" >
#{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedId != null" >
#{modifiedId,jdbcType=BIGINT},
</if>
<if test="modifiedTime != null" >
#{modifiedTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<insert id="insertSelective" parameterType="com.pica.cloud.account.account.server.entity.AccountContact">
insert into acct_contact
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="acctId != null">
acct_id,
</if>
<if test="mobilePhone != null">
mobile_phone,
</if>
<if test="deleteFlag != null">
delete_flag,
</if>
<if test="createdId != null">
created_id,
</if>
<if test="createdTime != null">
created_time,
</if>
<if test="modifiedId != null">
modified_id,
</if>
<if test="modifiedTime != null">
modified_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=BIGINT},
</if>
<if test="acctId != null">
#{acctId,jdbcType=INTEGER},
</if>
<if test="mobilePhone != null">
#{mobilePhone,jdbcType=VARCHAR},
</if>
<if test="deleteFlag != null">
#{deleteFlag,jdbcType=INTEGER},
</if>
<if test="createdId != null">
#{createdId,jdbcType=BIGINT},
</if>
<if test="createdTime != null">
#{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedId != null">
#{modifiedId,jdbcType=BIGINT},
</if>
<if test="modifiedTime != null">
#{modifiedTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<!--修改手机号-->
<update id="updateByPrimaryKeySelective" parameterType="com.pica.cloud.account.account.server.entity.AccountContact" >
update acct_contact
<set >
<if test="mobilePhone != null" >
mobile_phone = #{mobilePhone,jdbcType=VARCHAR},
</if>
<if test="deleteFlag != null" >
delete_flag = #{deleteFlag,jdbcType=INTEGER},
</if>
<if test="createdId != null" >
created_id = #{createdId,jdbcType=BIGINT},
</if>
<if test="createdTime != null" >
created_time = #{createdTime,jdbcType=TIMESTAMP},
</if>
<if test="modifiedId != null" >
modified_id = #{modifiedId,jdbcType=BIGINT},
</if>
<if test="modifiedTime != null" >
modified_time = #{modifiedTime,jdbcType=TIMESTAMP},
</if>
</set>
where acct_id = #{acctId,jdbcType=BIGINT}
</update>
</mapper>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册