zoukankan      html  css  js  c++  java
  • 阿里 短信认证

    package com.yetech;
    
    import java.util.Random;
    
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
    import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.aliyuncs.profile.IClientProfile;
    
    public class SignDemo {
        
        static final String product = "Dysmsapi";//产品名称
        static final String domain = "dysmsapi.aliyuncs.com";//产品域名
        
        public static SendSmsResponse sendSms(String phone, String code, String accessKeyId, String accessKeySecret) throws ClientException {
            //可自助调整超时时间10s
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");
            
            //初始化acsClient
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            
            //组装请求对象
            SendSmsRequest request = new SendSmsRequest();
            //必填
            request.setPhoneNumbers(phone);//待发送手机号
            request.setSignName("");//自己定义的短信签名
            request.setTemplateCode("");//自己定义的短信模板
            //可选
            request.setTemplateParam("{"code":""+code+""}");
            SendSmsResponse sendSmsResponse = null;
            try {
                sendSmsResponse = acsClient.getAcsResponse(request); 
                System.out.println(sendSmsResponse.getMessage() + "------" + sendSmsResponse.getRequestId());
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
                System.out.println("短信发送成功!");
            }else {
                    System.out.println("短信发送失败!");
            }
            return sendSmsResponse;
        }
        
        public static void main(String[] args) {
            try {
                Random random = new Random();
                String code = String.valueOf(random.nextInt(9000) + 1000);
                SignDemo.sendSms("15270170013",code,"你的accessKeyId","你的accessKeySecret");
            } catch (ClientException e) {
                e.printStackTrace();
            }
        }
        
    }

    需要的阿里jar包

    链接:https://pan.baidu.com/s/1wuA3ILwZwp7KD4Bg0n4NWA 

    提取码:nsuf

  • 相关阅读:
    【福利】JetBrains 全家桶永久免费使用
    openlayers操作分享:如何从容的在vue中食用openlayers6
    Axios的正确食用方法
    uniapp 地图全解析+事件监听
    JS数据扁平化
    Codeforces 记录
    WP开发加载图片及文字Resource/Content说明
    根据属性获取属性名
    string和byte 数组转换的快速方法(采用unsafe)
    Code First Migrations更新数据库结构(数据迁移)
  • 原文地址:https://www.cnblogs.com/ch94/p/10371219.html
Copyright © 2011-2022 走看看