提交 9d008dd3 编写于 作者: Chongwen.jiang's avatar Chongwen.jiang

短信服务调用失败抛异常,密码登录-锁定24小时逻辑添加

上级 640e6b8f
流水线 #22577 已失败 于阶段
in 0 second
......@@ -47,6 +47,8 @@ public class Constants {
/** 密码登录错误次数缓存key */
public static final String PWD_ERROR_NUM_KEY = "pwd-error-{mobile}";
/** 账号锁定缓存key */
public static final String ACCOUNT_LOCK_KEY = "account-lock-{mobile}";
/** 密码登录错误次数缓存时长*/
public static final int PWD_ERROR_NUM_SECONDS = 24 * 60 * 60;
......
package com.pica.cloud.account.account.server.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.account.account.server.configuration.PropertiesConfiguration;
import com.pica.cloud.account.account.server.constants.Constants;
......@@ -9,6 +10,7 @@ import com.pica.cloud.account.account.server.enums.AccountExceptionEnum;
import com.pica.cloud.account.account.server.exception.AccountException;
import com.pica.cloud.account.account.server.util.PICAPSendMsgModel;
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.entity.PicaWarnException;
import com.pica.cloud.foundation.utils.constants.SysCode;
......@@ -129,13 +131,27 @@ public abstract class AccountBaseController extends BaseController {
String postData = JSON.toJSONString(picapSendMsgModel);
String jsonObj = HttpClientUtil.httpExecute(messageUrl, postData);
if (StringUtils.isNotBlank(jsonObj)) {
logger.info("发送短信成功,返回结果:{}", jsonObj);
} else {
logger.error("发送短信失败");
if (StringUtils.isEmpty(jsonObj)) {
logger.error("send message fail, response is empty");
throw new PicaWarnException(AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getCode(),
AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getMessage());
}
try {
PicaResponse resp = JSON.parseObject(jsonObj, PicaResponse.class);
if(Objects.nonNull(resp) && PicaResultCode.SUCCESS.code().equals(resp.getCode())){
logger.info("send message success, the result is: {}", jsonObj);
} else {
logger.error("send message fail, response code not equals success code");
throw new PicaWarnException(AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getCode(),
AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getMessage());
}
} catch (JSONException e) {
logger.error("send message fail, response object is not a json object");
throw new PicaWarnException(AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getCode(),
AccountExceptionEnum.PICA_MESSAGE_SERVICE_CALL_FAIL.getMessage());
}
}
/**
......
......@@ -104,7 +104,7 @@ public class PasswordController extends AccountBaseController {
} else {
// 新版-已注册了的则找回密码
reSetPwd(request, account);
// 后台生成token返回
// 后台生成token返回,删除账号锁定缓存
return PicaResponse.toResponse(passwordService.findPwdLogin(request, account));
}
} else {
......
......@@ -136,25 +136,33 @@ public class LoginServiceImpl implements LoginService {
AccountExceptionEnum.PICA_PASSWORD_RULE_ERROR.getMessage()
.replace("{mobile}", mobile));
} else {
// 判断账号是否已锁
logger.info("new login failure:" + mobile);
String lockKey = Constants.ACCOUNT_LOCK_KEY.replace("{mobile}", mobile);
if (redisClient.exists(lockKey)) {
throw new PicaException(AccountExceptionEnum.PICA_PWD_MISMATCH_5.getCode(),
AccountExceptionEnum.PICA_PWD_MISMATCH_5.getMessage().replace("{mobile}", mobile));
}
if (password.equals(oldPwd)) {
return pwdLoginCorrect(request, mobile, encrypt, accountInfoEntity);
} else {
// 从缓存取出当前账号密码错误次数
logger.info("new login failure:" + mobile);
String key = Constants.PWD_ERROR_NUM_KEY.replace("{mobile}", mobile);
if (redisClient.exists(key)) {
int errorCount = Integer.parseInt(redisClient.get(key));
String errorKey = Constants.PWD_ERROR_NUM_KEY.replace("{mobile}", mobile);
if (redisClient.exists(errorKey)) {
int errorCount = Integer.parseInt(redisClient.get(errorKey));
errorCount = errorCount + 1;
redisClient.set(key, errorCount, Constants.PWD_ERROR_NUM_SECONDS);
redisClient.set(errorKey, errorCount, Constants.PWD_ERROR_NUM_SECONDS);
if (errorCount <= 4) {
throw new PicaException(AccountExceptionEnum.PICA_PWD_MISMATCH_4.getCode(),
AccountExceptionEnum.PICA_PWD_MISMATCH_4.getMessage());
} else {
// 设置账号锁定24h
redisClient.set(lockKey, mobile, Constants.PWD_ERROR_NUM_SECONDS);
throw new PicaException(AccountExceptionEnum.PICA_PWD_MISMATCH_5.getCode(),
AccountExceptionEnum.PICA_PWD_MISMATCH_5.getMessage().replace("{mobile}", mobile));
}
} else {
redisClient.set(key, 1, Constants.PWD_ERROR_NUM_SECONDS);
redisClient.set(errorKey, 1, Constants.PWD_ERROR_NUM_SECONDS);
throw new PicaException(AccountExceptionEnum.PICA_PWD_MISMATCH_4.getCode(),
AccountExceptionEnum.PICA_PWD_MISMATCH_4.getMessage());
}
......
package com.pica.cloud.account.account.server.service.impl;
import com.pica.cloud.account.account.server.constants.Constants;
import com.pica.cloud.account.account.server.entity.*;
import com.pica.cloud.account.account.server.enums.AccountExceptionEnum;
import com.pica.cloud.account.account.server.enums.AccountTypeEnum;
......@@ -15,6 +16,7 @@ import com.pica.cloud.account.account.server.util.AccountUtils;
import com.pica.cloud.account.account.server.util.TokenUtils;
import com.pica.cloud.foundation.encryption.common.constants.EncryptConstants;
import com.pica.cloud.foundation.encryption.util.EncryptUtils;
import com.pica.cloud.foundation.redis.ICacheClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -46,6 +48,9 @@ public class PasswordServiceImpl implements PasswordService {
@Autowired
private DoctorMapper doctorInfoMapper;
@Autowired
private ICacheClient cacheClient;
@Override
@Transactional
......@@ -105,7 +110,7 @@ public class PasswordServiceImpl implements PasswordService {
}
/**
* @Description 重置密码-自动登录(生成token)
* @Description 重置密码-自动登录(生成token),删除账号锁定缓存
* @Author Chongwen.jiang
* @Date 2020/3/3 17:44
* @ModifyDate 2020/3/3 17:44
......@@ -127,11 +132,16 @@ public class PasswordServiceImpl implements PasswordService {
result.setUserId(userId);
result.setMobile(request.getMobile());
result.setDoctorId(EncryptUtils.encryptContent(userId + "", EncryptConstants.ENCRYPT_TYPE_ID));
//是否完善过个人信息(云鹊医app才需要)
// 是否完善过个人信息(云鹊医app才需要)
if (request.getProductType() == AccountTypeEnum.PRODUCT_TYPE_DOCTOR.getCode()) {
Doctor doctorEntity = doctorInfoMapper.selectByPrimaryKey(userId.intValue());
result.setEntireFlag(doctorEntity.getEntireFlag());
}
// 删除账号锁定缓存
String lockKey = Constants.ACCOUNT_LOCK_KEY.replace("{mobile}", request.getMobile());
if(cacheClient.exists(lockKey)) {
cacheClient.del(lockKey);
}
return result;
}
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册