缓存模块提供smember接口+短信签名校验实现

main
heqijun 4 months ago
parent e4153bb349
commit 54949890b6

@ -4,6 +4,7 @@ import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* @author heqijun * @author heqijun
@ -36,4 +37,6 @@ public interface CacheClient {
@PostMapping("/sadd/{key}") @PostMapping("/sadd/{key}")
void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String, Object>... maps); void sadd(@PathVariable(value = "key") String key, @RequestBody Map<String, Object>... maps);
@PostMapping("/smember/{key}")
Set<Map> smember(@PathVariable(value = "key") String key);
} }

@ -31,11 +31,11 @@ public class ApikeySendCheckServiceImpl implements SendCheckService {
@Override @Override
public void check(StandardSubmit submit) { public void check(StandardSubmit submit) {
log.info("【api模块】发送短信前apikey校验。。。"); log.info("【接口模块】发送短信前apikey校验。。。");
Map<String, String> clientBusinessMap = (Map<String, String>) cacheClient.hget(CacheConstant.CLIENT_BUSINESS + submit.getApikey()); Map<String, String> clientBusinessMap = (Map<String, String>) cacheClient.hget(CacheConstant.CLIENT_BUSINESS + submit.getApikey());
log.info("缓存中查询到客户信息:{}", clientBusinessMap); log.info("【接口模块】缓存中查询到客户信息:{}", clientBusinessMap);
if (MapUtils.isEmpty(clientBusinessMap)) { if (MapUtils.isEmpty(clientBusinessMap)) {
log.error("apikey校验不通过{}{}", submit.getApikey(), ExceptionEnums.ERROR_APIKEY.getMsg()); log.error("【接口模块】apikey校验不通过{}{}", submit.getApikey(), ExceptionEnums.ERROR_APIKEY.getMsg());
throw new ApiException(ExceptionEnums.ERROR_APIKEY); throw new ApiException(ExceptionEnums.ERROR_APIKEY);
} }
@ -44,7 +44,6 @@ public class ApikeySendCheckServiceImpl implements SendCheckService {
// BeanUtils.copyProperties(clientBusiness, submit); // BeanUtils.copyProperties(clientBusiness, submit);
submit.setClientId(clientBusiness.getId()); submit.setClientId(clientBusiness.getId());
// submit.setIpAddress(clientBusiness.getIpAddress()); // submit.setIpAddress(clientBusiness.getIpAddress());
log.info("【接口模块】apikey校验通过{}", submit.getApikey());
log.info("apikey校验通过{}", submit.getApikey());
} }
} }

@ -1,11 +1,20 @@
package com.mashibing.api.service.sendCheck.impl; package com.mashibing.api.service.sendCheck.impl;
import com.mashibing.api.feignClient.CacheClient;
import com.mashibing.api.pojo.SingleSendRequest; import com.mashibing.api.pojo.SingleSendRequest;
import com.mashibing.api.service.sendCheck.SendCheckService; import com.mashibing.api.service.sendCheck.SendCheckService;
import com.mashibing.common.constant.CacheConstant;
import com.mashibing.common.constant.SMSConstant;
import com.mashibing.common.enums.ExceptionEnums;
import com.mashibing.common.exception.ApiException;
import com.mashibing.common.pojo.StandardSubmit; import com.mashibing.common.pojo.StandardSubmit;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Set;
/** /**
* @author heqijun * @author heqijun
* @ClassName: SignSendCheckServiceImpl * @ClassName: SignSendCheckServiceImpl
@ -16,8 +25,43 @@ import org.springframework.stereotype.Service;
@Slf4j @Slf4j
@Service("SignSendCheck") @Service("SignSendCheck")
public class SignSendCheckServiceImpl implements SendCheckService { public class SignSendCheckServiceImpl implements SendCheckService {
@Autowired
CacheClient cacheClient;
@Override @Override
public void check(StandardSubmit standardSubmit) { public void check(StandardSubmit standardSubmit) {
log.info("Check SignSendCheck"); log.info("【接口模块】短信客户签名校验。。。");
String text = standardSubmit.getText();
//短信正确格式:【签名】短信内容。。。
//短信内容为空,显然不可能包含签名
if (text == null || text.isEmpty()) {
log.info("【接口模块-校验签名】短信无可用签名 text = {}", text);
throw new ApiException(ExceptionEnums.ERROR_SIGN);
}
//短信不以“【”开头或者不包含“】”,签名格式不正确
if (!text.startsWith(SMSConstant.SIGN_PREFIX) || !text.contains(SMSConstant.SIGN_PREFIX)) {
log.info("【接口模块-校验签名】短信无可用签名 text = {}", text);
throw new ApiException(ExceptionEnums.ERROR_SIGN);
}
//截取签名
String signInText = text.substring(1, text.indexOf(SMSConstant.SIGN_SUFFIX));
//查询缓存中的客户签名信息
Set<Map> clientSigns = cacheClient.smember(CacheConstant.CLIENT_SIGN + standardSubmit.getClientId());
if (clientSigns == null || clientSigns.isEmpty()) {
log.info("【接口模块-校验签名】客户无可用签名");
throw new ApiException(ExceptionEnums.ERROR_SIGN);
}
//取出签名
for (Map clientSign : clientSigns) {
String signInCache = (String) clientSign.get("signInfo");
if (signInCache.equals(signInText)) {
log.info("【接口模块】签名校验通过text = {}", text);
return;
}
}
log.info("【接口模块-校验签名】短信无可用签名 text = {}", text);
throw new ApiException(ExceptionEnums.ERROR_SIGN);
} }
} }

@ -7,6 +7,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.Map; import java.util.Map;
import java.util.Set;
/** /**
* @author heqijun * @author heqijun
@ -69,4 +70,12 @@ public class CacheController {
redisClient.sAdd(key, value); redisClient.sAdd(key, value);
} }
@PostMapping("/smember/{key}")
public Set smember(@PathVariable(value = "key") String key) {
log.info("【缓存模块】smember: key = {}", key);
Set value = redisClient.sMembers(key);
log.info("【缓存模块】sadd: key = {}\nvalue = {}", key, value);
return value;
}
} }

@ -3,7 +3,7 @@ package com.mashibing.common.constant;
/** /**
* @author heqijun * @author heqijun
* @ClassName: Constant * @ClassName: Constant
* @Description: ip * @Description:
* @date 2025/6/5 17:10 * @date 2025/6/5 17:10
*/ */
@ -13,4 +13,8 @@ public class SMSConstant {
public static final String X_FORWARDED_FOR = "x-forwarded-for"; public static final String X_FORWARDED_FOR = "x-forwarded-for";
public static final String SIGN_PREFIX = "【";
public static final String SIGN_SUFFIX = "】";
} }

@ -22,7 +22,7 @@ public class StandardSubmit {
@Description("当前短信的唯一标识") @Description("当前短信的唯一标识")
private Long sequenceId; private Long sequenceId;
@Description("客户ID") @Description("客户ID")
private Long clientId; private Long clientId;
@Description("客户端的ip白名单") @Description("客户端的ip白名单")

Loading…
Cancel
Save