手机号码白名单

本文探讨了如何通过Redis缓存加速手机号白名单验证,避免全量查询,同时考虑黑名单和屏蔽规则,以及在项目启动时如何加载和使用白名单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手机号码白名单功能:

思路:在发送时与数据库白名单表做校验,为白名单号码无阻发送

缺点:如果一次性发送十几万号码,每个号码都要和白名单库做校验,速度极大降低

,而且可以采用如果是超频,黑名单,或者屏蔽地区的号码的条件下,再做白名单校验

实例:在项目启动时,加载白名单数据到redis内存中,每次发送从redis缓存中获取白名单号码做校验

1、白名单常量类

/**
 * @Description SMS模块缓存键值常量
 * @Date 2021-06-01 18:26
 * @Created by Lxq
 */
public class SmsCacheConst {

    /**商家应用信息缓存键值*/
    //public static final String CLINETAPP_APPID = "mms:clientApp:appid";
    public static final String CLINETAPP_ID = "mms:clientApp:id";
    /**通道缓存键值*/
    public static final String CHANNEL_ID = "mms:masGdZw:channel";
    /**缓存中的黑名单库*/
    public static final String BLACK_IDCODE = "mms:black";

    /**Redis缓存中白名单号码的键值 */
    public static final String WHITE_MOBILES = "SmsWhiteMobile";
}

2、启动时获取白名单号码,并缓存到redis中

package com.chuangci.cxcsp.smsapi.component;

import com.chuangci.cxcsp.common.utils.RedisUtils;
import com.chuangci.cxcsp.sms.domain.SmsWhite;
import com.chuangci.cxcsp.sms.service.ISmsWhiteService;
import com.chuangci.cxcsp.smsapi.constant.SmsCacheConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;


@Slf4j
@Component
public class WhiteMobileLoad implements ApplicationRunner {

    @Autowired
    RedisUtils redisUtils;
    @Autowired
    ISmsWhiteService smsWhiteService;

    @Override
    @Async
    public void run(ApplicationArguments args) throws Exception {

        log.info("开始加载号码白名单到缓存");
        List<SmsWhite> whiteList = smsWhiteService.selectSmsWhiteList(new SmsWhite());
        if (whiteList==null){
            log.warn("不存在白名单号码");
            return;
        }

        redisUtils.del(SmsCacheConst.WHITE_MOBILES);
        log.info("删除已存在的白名单缓存");

        for (SmsWhite smsWhite : whiteList) {
            redisUtils.sSet(SmsCacheConst.WHITE_MOBILES,smsWhite.getIdcode());
        }
        log.info("加载号码白名单到缓存完成,共{}个",whiteList.size());

    }
}

3、发送时获取从redis中获取白名单号码

  //从缓存获取白名单号码数据
        Set<Object> whiteMobiles = redisUtils.sGet(SmsCacheConst.WHITE_MOBILES);
        if (whiteMobiles == null){
            whiteMobiles = new HashSet<>();
        }

4、发送时校验,非白名单号码做校验

  if (!whiteMobiles.contains(mobile)) {
                    //屏蔽地区号码校验、处理
                    if (StringUtils.isNotBlank(shieldedArea)) {
                        String geo = PhoneUtils.getAreaName(mobile);
                        String province = PhoneUtils.getProvince(mobile);
                        if (ArrayUtil.contains(shieldedAreaArray, geo) || ArrayUtil.contains(shieldedAreaArray, province)) {
                            sendResult.setMobileValid(4);
                            sendResult.setStatusCode("CX:0004");
                            sendResult.setStatusDesc("屏蔽地区号码");
                            shieldNum++;
                            continue;
                        }
                        if (allowAreaArray != null && allowAreaArray.length > 0 && !(ArrayUtil.contains(allowAreaArray, geo) || ArrayUtil.contains(allowAreaArray, province))) {
                            sendResult.setMobileValid(7);
                            sendResult.setStatusCode("CX:0007");
                            sendResult.setStatusDesc("非白名单区域号码");
                            continue;
                        }
                    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值