SpringBoot调用阿里云接口发送短信

一:课前准备

需要上阿里云官网申请

accessKeyId、secret、templateCode、signName、

二:依赖的maven坐标(这里我把代码中用到的全供上)


        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.aliyun
            aliyun-java-sdk-core
            4.5.1
        

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
        
            com.alibaba
            fastjson
            1.2.62
        

        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

三、模拟真实的业务场景发送短信验证码

(1)、Controller层:

@RestController
//跨域支持
@CrossOrigin
public class SmsApiController {
    @Autowired
    private SendSms sendSms;
    @Autowired
    private RedisTemplate redisTemplate;


    @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 param = new HashMap<>();
        //name是配置在模板中的参数
        param.put("name", code);

        boolean isSend = sendSms.send(phone, param);

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

(2)、Service层:

public interface SendSms {
    public boolean send(String phoneNum, Map code);
}

     ServiceImpl

@Service
public class SendSmsImpl implements SendSms {
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${secret}")
    private String secret;
    @Value("${signName}")
    private String signName;
    @Value("${templateCode}")
    private String templateCode;


    @Override
    public boolean send(String phoneNum, Map code) {
        //连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        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", signName);//签名(要和之前申请的保持一致)
        request.putQueryParameter("TemplateCode", templateCode);//模板管理里的模板CODE

        //构建一个短信的验证码
        request.putQueryParameter("TemplateParam", JSON.toJSONString(code));
        request.putQueryParameter("RegionId", "cn-hangzhou");


        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;
    }
}

 

你可能感兴趣的:(Java)