阿里云短信服务

阿里云短信服务

  • 阿里云登录
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SendSms {
    public static void main(String[] args) {
        // 连接阿里云
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");// 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", "手机号");
        request.putQueryParameter("SignName", "签名");
        request.putQueryParameter("TemplateCode", "模板Code");
        // 构建短信验证码
        HashMap<String,Object> map = new HashMap<>();
        map.put("code","1122");
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        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;
    }
}
		<!--阿里云短信依赖-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.3</version>
        </dependency>
            
        <!--fastJSON-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.73</version>
        </dependency>
            
        <!--结合Redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
@CrossOrigin // 控制台跨域支持
  • 调用短信业务

真实场景下:调用短信业务+添加redis

  1. application.propertities中添加Redis配置

    # Redis的配置
    spring.redis.host=localhost
    spring.redis.port=6379
    spring.redis.password=
    
  2. 打开Redis缓存软件

  3. 进行代码书写

calhost
spring.redis.port=6379
spring.redis.password=

在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值