短信-获取时校验封禁

This commit is contained in:
khalil
2024-07-19 13:13:25 +08:00
parent 2e75c9fb74
commit 5ba0e4e015
4 changed files with 173 additions and 102 deletions

View File

@@ -0,0 +1,150 @@
package com.accompany.core.service.account;
import cn.hutool.core.util.StrUtil;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.utils.DateTimeUtil;
import com.accompany.core.constant.BlockStatusEnum;
import com.accompany.core.constant.BlockTypeEnum;
import com.accompany.core.model.AccountBlock;
import com.accompany.core.service.common.JedisService;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Calendar;
/**
* @author xiaoyuyou
* @date 2020/03/18 09:50
*/
@Service
@Slf4j
public class AccountBlockCheckService {
@Autowired
private JedisService jedisService;
private Gson gson = new Gson();
public boolean checkBlocked(String val, BlockTypeEnum blockTypeEnum) {
int blockType = blockTypeEnum.getValue();
String accountCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), val);
if (!StringUtils.hasText(accountCache)){
return false;
}
AccountBlock accountBlock = gson.fromJson(accountCache, AccountBlock.class);
boolean betweenDate = DateTimeUtil.isBetweenDate(Calendar.getInstance().getTime(), accountBlock.getBlockStartTime(), accountBlock.getBlockEndTime());
if (betweenDate && accountBlock.getBlockStatus() != null
&& BlockStatusEnum.BLOCKING.getValue() == accountBlock.getBlockStatus().byteValue()) {
log.info("用户被封禁,blockValue =" + accountBlock.getBlockValue());
return true;
}
return false;
}
public boolean checkBlockedErbanNo(Long erbanNo){
if (null == erbanNo){
return false;
}
String erbanNoStr = String.valueOf(erbanNo);
return checkBlocked(erbanNoStr, BlockTypeEnum.BLOCK_ACCOUNT);
}
public boolean checkBlockedPhone(String phone){
if (!StringUtils.hasText(phone)){
return false;
}
return checkBlocked(phone, BlockTypeEnum.BLOCK_PHONE);
}
/**
* 查询设备是否被封禁
* @param deviceId
*/
public boolean checkBlockedDevice(String deviceId) {
if (StrUtil.isEmpty(deviceId)) {
return false;
}
return checkBlocked(deviceId, BlockTypeEnum.BLOCK_DEVICE);
}
public boolean checkBlockedIp(String ip) {
if (StrUtil.isEmpty(ip)) {
return false;
}
return checkBlocked(ip, BlockTypeEnum.BLOCK_IP);
}
public Long checkReturnEndTime(Long erbanNo, String phone, String deviceId, String ip){
Long endTime = checkBlockedErbanNoReturnBlockEndTime(erbanNo);
if (null != endTime){
return endTime;
}
endTime = checkBlockedPhoneReturnBlockEndTime(phone);
if (null != endTime){
return endTime;
}
endTime = checkBlockedDeviceReturnBlockEndTime(deviceId);
if (null != endTime){
return endTime;
}
return checkBlockedIpReturnBlockEndTime(ip);
}
public Long checkBlockedReturnBlockEndTime(String val, BlockTypeEnum blockTypeEnum) {
int blockType = blockTypeEnum.getValue();
String accountCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), val);
if (!StringUtils.hasText(accountCache)){
return null;
}
AccountBlock accountBlock = gson.fromJson(accountCache, AccountBlock.class);
boolean betweenDate = DateTimeUtil.isBetweenDate(Calendar.getInstance().getTime(), accountBlock.getBlockStartTime(), accountBlock.getBlockEndTime());
if (betweenDate && accountBlock.getBlockStatus() != null
&& BlockStatusEnum.BLOCKING.getValue() == accountBlock.getBlockStatus().byteValue()) {
log.info("用户被封禁,blockValue =" + accountBlock.getBlockValue());
return accountBlock.getBlockEndTime().getTime();
}
return null;
}
public Long checkBlockedErbanNoReturnBlockEndTime(Long erbanNo){
if (null == erbanNo){
return null;
}
String erbanNoStr = String.valueOf(erbanNo);
return checkBlockedReturnBlockEndTime(erbanNoStr, BlockTypeEnum.BLOCK_ACCOUNT);
}
public Long checkBlockedPhoneReturnBlockEndTime(String phone){
if (!StringUtils.hasText(phone)){
return null;
}
return checkBlockedReturnBlockEndTime(phone, BlockTypeEnum.BLOCK_PHONE);
}
/**
* 查询设备是否被封禁
* @param deviceId
*/
public Long checkBlockedDeviceReturnBlockEndTime(String deviceId) {
if (StrUtil.isEmpty(deviceId)) {
return null;
}
return checkBlockedReturnBlockEndTime(deviceId, BlockTypeEnum.BLOCK_DEVICE);
}
public Long checkBlockedIpReturnBlockEndTime(String ip) {
if (StrUtil.isEmpty(ip)) {
return null;
}
return checkBlockedReturnBlockEndTime(ip, BlockTypeEnum.BLOCK_IP);
}
}

View File

@@ -9,7 +9,7 @@ import com.accompany.common.utils.CommonUtil;
import com.accompany.common.utils.IPUtils;
import com.accompany.core.exception.ServiceException;
import com.accompany.core.model.Users;
import com.accompany.core.service.phone.PhoneAuthApplyRecordService;
import com.accompany.core.service.account.AccountBlockCheckService;
import com.accompany.core.service.user.PhoneBlackService;
import com.accompany.core.service.user.UsersBaseService;
import com.accompany.sms.service.SmsService;
@@ -40,14 +40,13 @@ public class SmsController extends BaseController {
private static final Logger logger = LoggerFactory.getLogger(SmsController.class);
@Autowired
SmsService smsService;
private SmsService smsService;
@Autowired
private UsersBaseService usersBaseService;
@Autowired
private PhoneBlackService phoneBlackService;
@Autowired
private PhoneAuthApplyRecordService phoneAuthApplyRecordService;
private AccountBlockCheckService accountBlockCheckService;
private final static List<Integer> USE_PHONE_IN_PARAM_TYPES = Arrays.asList(SmsTypeEnum.REGISTER.value, SmsTypeEnum.LOGIN.value, SmsTypeEnum.SUPER_ADMIN_LOGIN.value,
SmsTypeEnum.RESET_PASSWORD_FOR_NO_LOGIN.value, SmsTypeEnum.BINDING_PHONE.value);
@@ -80,6 +79,14 @@ public class SmsController extends BaseController {
return SmsTypeEnum.REGISTER.getValue() == type ? new BusiResult<>(BusiStatus.SMS_SEND_SUCCESS) :
new BusiResult<>(BusiStatus.PHONE_INVALID);
}
//检查账号、设备号、号段是否封禁
if (accountBlockCheckService.checkBlockedDevice(deviceInfo.getDeviceId())
|| accountBlockCheckService.checkBlockedIp(ip)
|| accountBlockCheckService.checkBlockedPhone(mobile)){
throw new ServiceException(BusiStatus.ACCOUNT_ERROR);
}
return smsService.sendSmsCode(mobile, type, deviceInfo, ip, null);
}

View File

@@ -22,7 +22,7 @@ import com.accompany.core.service.user.UsersBaseService;
import com.accompany.oauth2.constant.LoginTypeEnum;
import com.accompany.oauth2.exception.CustomOAuth2Exception;
import com.accompany.oauth2.model.AccountDetails;
import com.accompany.oauth2.service.account.AccountBlockCheckService;
import com.accompany.core.service.account.AccountBlockCheckService;
import com.accompany.oauth2.service.account.AccountManageService;
import com.accompany.oauth2.util.RequestContextHolderUtils;
import com.accompany.sms.service.SmsService;
@@ -139,16 +139,21 @@ public class MyUserDetailsServiceImpl implements MyUserDetailsService {
if (users != null && NEED_INTERCEPT_USER_TYPE.contains(users.getDefUser())) {
throw new ServiceException(BusiStatus.ILLEGAL_OPERATE);
}
//检查账号是否封禁
accountBlockCheckService.checkBlockedAccount(account);
//检查设备是否封禁
accountBlockCheckService.checkBlockedDevice(deviceId);
//检查设备号是否被封禁
accountBlockCheckService.checkBlockedIp(ip);
// 检查账号是否在号段黑名单
if (phoneBlackService.checkIsNeedIntercept(account.getPhone())) {
throw new ServiceException(BusiStatus.PHONE_BE_INTERCEPTED);
}
Long blockEndTime = accountBlockCheckService.checkReturnEndTime(account.getErbanNo(), account.getPhone(), deviceId, ip);
//检查账号、设备号、号段是否封禁
if (null != blockEndTime){
CustomOAuth2Exception exception = new CustomOAuth2Exception(CustomOAuth2Exception.ACCOUNT_ERROR, "");
exception.addAdditionalInformation("reason", "违规请联系客服WeChat:sd245376");
exception.addAdditionalInformation("date", String.valueOf(blockEndTime));
throw exception;
}
//校验验证码
checkSmsCodeByUserType(account, smsCode, loginType, deviceInfo.getApp());
accountManageService.checkAccountCancel(uid);

View File

@@ -1,91 +0,0 @@
package com.accompany.oauth2.service.account;
import cn.hutool.core.util.StrUtil;
import com.accompany.common.redis.RedisKey;
import com.accompany.common.utils.DateTimeUtil;
import com.accompany.core.constant.BlockStatusEnum;
import com.accompany.core.constant.BlockTypeEnum;
import com.accompany.core.model.Account;
import com.accompany.core.model.AccountBlock;
import com.accompany.core.service.common.JedisService;
import com.accompany.oauth2.exception.CustomOAuth2Exception;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Calendar;
/**
* @author xiaoyuyou
* @date 2020/03/18 09:50
*/
@Service
@Slf4j
public class AccountBlockCheckService {
@Autowired
private JedisService jedisService;
private Gson gson = new Gson();
/**
* 查询账号设备是否被封禁
*/
public void checkBlockedAccount(Account account) {
String erbanNo = String.valueOf(account.getErbanNo());
Integer blockType = BlockTypeEnum.BLOCK_ACCOUNT.getValue();
String accountCache = jedisService.hget(RedisKey.block_account.getKey(blockType.toString()), erbanNo);
if (!StringUtils.isEmpty(accountCache)) {
checkAccountBlock(accountCache);
}
//如果账号封禁没有,继续去手机号封禁查询
String phone = account.getPhone();
blockType = BlockTypeEnum.BLOCK_PHONE.getValue();
accountCache = jedisService.hget(RedisKey.block_account.getKey(blockType.toString()), phone);
if (!StringUtils.isEmpty(accountCache)) {
checkAccountBlock(accountCache);
}
}
/**
* 查询设备是否被封禁
* @param deviceId
*/
public void checkBlockedDevice(String deviceId) {
if (StrUtil.isEmpty(deviceId)) {
return;
}
int blockType = BlockTypeEnum.BLOCK_DEVICE.getValue();
String deviceCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), deviceId);
if (StrUtil.isNotEmpty(deviceCache)) {
checkAccountBlock(deviceCache);
}
}
public void checkBlockedIp(String ip) {
if (StrUtil.isEmpty(ip)) {
return;
}
int blockType = BlockTypeEnum.BLOCK_IP.getValue();
String ipCache = jedisService.hget(RedisKey.block_account.getKey(Integer.toString(blockType)), ip);
if (StrUtil.isNotEmpty(ipCache)) {
checkAccountBlock(ipCache);
}
}
private void checkAccountBlock(String accountBlockCache) {
AccountBlock accountBlock = gson.fromJson(accountBlockCache, AccountBlock.class);
boolean betweenDate = DateTimeUtil.isBetweenDate(Calendar.getInstance().getTime(), accountBlock.getBlockStartTime(), accountBlock.getBlockEndTime());
if (betweenDate && accountBlock.getBlockStatus() != null && BlockStatusEnum.BLOCKING.getValue() == accountBlock.getBlockStatus().byteValue()) {
log.info("用户被封禁,blockValue =" + accountBlock.getBlockValue());
BlockTypeEnum blockTypeEnum = BlockTypeEnum.get(accountBlock.getBlockType());
CustomOAuth2Exception exception = new CustomOAuth2Exception(CustomOAuth2Exception.ACCOUNT_ERROR, blockTypeEnum.getBlockDesc());
exception.addAdditionalInformation("reason", "违规请联系客服WeChat:sd245376");
exception.addAdditionalInformation("date", String.valueOf(accountBlock.getBlockEndTime().getTime()));
throw exception;
}
}
}