Springboot se da cuenta de que envía un código de verificación por SMS y un código de verificación de verificación anti-cepillo

El código de verificación que usamos aquí es Alibaba Cloud SMS
Inserte la descripción de la imagen aquí
https://market.aliyun.com/products/57000002/cmapi00039249.html?spm=5176.2020520132.101.3.53e87218NGtSXW#sku=yuncode3324900001
La dirección de interfaz proporcionada por el proveedor de servicios, la solicitud Los parámetros son todos diferentes, consulte el código de prueba proporcionado por el proveedor de servicios.

Pruebe el envío del código de verificación por SMS, la demostración aquí se puede copiar desde Alibaba Cloud

@Test
public void contextLoads() {
    
    
   String host = "http://dingxin.market.alicloudapi.com";
	    String path = "/dx/sendSms";
	    String method = "POST";
	    String appcode = "你自己的AppCode";
	    Map<String, String> headers = new HashMap<String, String>();
	    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
	    headers.put("Authorization", "APPCODE " + appcode);
	    Map<String, String> querys = new HashMap<String, String>();
	    querys.put("mobile", "159xxxx9999");
	    querys.put("param", "code:1234");
	    querys.put("tpl_id", "TP1711063");
	    Map<String, String> bodys = new HashMap<String, String>();


	    try {
    
    
	    	/**
	    	* 重要提示如下:
	    	* HttpUtils请从
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
	    	* 下载
	    	*
	    	* 相应的依赖请参照
	    	* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
	    	*/
	    	HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
	    	System.out.println(response.toString());
	    	//获取response的body
	    	//System.out.println(EntityUtils.toString(response.getEntity()));
	    } catch (Exception e) {
    
    
	    	e.printStackTrace();
	    }
}

En el proyecto desarrollado, podemos llamar a thirdPartFeignService, el módulo de mensajes de texto que hemos escrito.

/**
 * 发送短信验证码
 * @param phone 手机号
 * @return
 */
@GetMapping("/sms/sendCode")
@ResponseBody
public R sendCode(@RequestParam("phone") String phone) {
    
    
    // TODO 1、接口防刷
    // 先从redis中拿取
    String redisCode = redisTemplate.opsForValue().get("sms:code:"+ phone);
    if(!StringUtils.isEmpty(redisCode)) {
    
    
        // 拆分
        long l = Long.parseLong(redisCode.split("_")[1]);
        // 当前系统事件减去之前验证码存入的事件 小于60000毫秒=60秒
        if (System.currentTimeMillis() -l < 60000) {
    
    
            // 60秒内不能再发
            R.error(BizCodeEnume.SMS_CODE_EXCEPTION.getCode(),BizCodeEnume.SMS_CODE_EXCEPTION.getMsg());
        }
    }
    // 2、验证码的再次效验
    // 数据存入 =》redis key-phone value - code sms:code:131xxxxx - >45678
    String code = UUID.randomUUID().toString().substring(0,5).toUpperCase();
    // 拼接验证码
    String substring = code+"_" + System.currentTimeMillis();
    // redis缓存验证码 防止同一个phone在60秒内发出多次验证吗
    redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,substring,10, TimeUnit.MINUTES);

    // 调用第三方服务模块发送验证码
    thirdPartFeignService.sendCode(phone,code);
    return R.ok();
}

Para evitar que alguien deslice maliciosamente SMS, aquí hemos agregado una función para evitar el deslizamiento en 60 segundos.

Supongo que te gusta

Origin blog.csdn.net/u014496893/article/details/113925258
Recomendado
Clasificación