使用阿里云短信验证

这里使用的是阿里云大于整合springboot实现短信的验证服务

登录阿里云,在首页中搜索‘短信服务’

 

我们需要拿到签名名称,模板CODE,accessKeyId,以及 accessSecret。

签名与模板需要审核,不过也很快。

 

废话少说,直接上代码

需要导入的依赖:

 <!--json数据-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <!--aliyun-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
        </dependency>
        <!--redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

 

service接口

public boolean send(String phoneNum, String templateCode, Map<String,Object> code);

 

业务层

@Override
    public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
        //连接阿里云,这里需要填写你阿里云的 accessKeyId, accessSecret
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "accessSecret");
        IAcsClient client = new DefaultAcsClient(profile);

        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        /**
         * 自定义参数
         * 手机号,验证码,签名,模板
         */
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "智丰健康");
        request.putQueryParameter("TemplateCode", templateCode);

        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            //成功返回
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }

 

controller层

 @GetMapping("/send/{phone}")
    public String code(@PathVariable("phone") String phone){
        //把号码存到redis中
        String code=redisTemplate.opsForValue().get(phone);

        //判断验证码是否已存在,按照正常的逻辑,验证码相同是不影响的
        if (!StringUtils.isEmpty(code)){
            return phone+ ":" +code+ "已存在,还没过期";
        }

        //自动生成验证码,并且存到redis中
        code= UUID.randomUUID().toString().substring(0,4);
        HashMap<String, Object> param = new HashMap<>();
        param.put("code",code);

        boolean isSend = sendSmsService.send(phone, "模版CODE(SMS_203176825)", param);

        if (isSend){
            redisTemplate.opsForValue().set(phone,code,5,TimeUnit.SECONDS);
            return phone+":" +code+"发送成功";
        }else{
            return "发送失败";
        }
    }
    

 

测试:(这里的api是Restful风格,访问路径跟以往的不一样)

访问路径为:http://localhost:9090/send/电话号码

 代码已托管码云:https://gitee.com/ckfeng/aliyun_note_verify

 

ok结束了,午安。

posted @ 2020-09-24 13:31  安详的苦丁茶  阅读(214)  评论(0编辑  收藏  举报