java整合阿里云短信服务实现验证码功能
java要想实现短信验证码功能,需要整合第三方短信服务API,如阿里、腾讯等短信服务,本文介绍的是整合阿里云短信服务实现验证码功能
一 登录阿里云短信服务平台登记信息
首先需要登录阿里云短信服务平台注册相关信息阿里云短信服务
- 绑定测试手机号
- 申请短信模版签名
需要先添加资质
点击添加资质
填写申请信息
审核结果需要大概两小时,会以短信形式通知结果。审核通过后,这里就有下拉选项可以选择了,然后填写其他信息,点击最下边的“提交即可”
等待结果即可(短信通知)
通过后,可以看到结果
点击详情可以看到短信模版
如果上边生成的模版不满足个人要求,可以自己再申请一个模版
填写模板信息
提交等待结果即可。通过后,点击左侧“快速学习和测试”,选择自己申请的签名和模版,“调用API发送短信”
修改验证码(自定义即可),点击“发起调用”
右侧可以看到,调用成功,并且手机收到了验证码
二 java代码集成阿里云短信服务发送手机验证码
登录阿里云,找到自己的access key
在上边这个页面,可以申请accesskey,申请后会得到自己的AccessKey ID和AccessKey Secret俩值,项目里会用到这俩值。
还是打开刚才的短信服务页面阿里云短信服务,点击下方的“调用API发送短信”
在右侧找到java的sdk示例
把示例复制到我们的idea开发工具里
在pom.xml里添加阿里云相关依赖
<!-- 阿里云短信依赖 -->
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-core</artifactId><version>4.5.16</version>
</dependency>
<dependency><groupId>com.aliyun</groupId><artifactId>aliyun-java-sdk-dysmsapi</artifactId><version>2.1.0</version>
</dependency><!-- 阿里云JSON -->
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.31</version>
</dependency>
application.yml配置文件如下:
sms:keyid: LTAI....... # AccessKey ID,改为自己的值,本博客里第二章开头申请的值keysecret: YYFwh.......... # AccessKey Secret,改为自己的值,本博客里第二章开头申请的值signname: ..... # 签名改为自己的值templatecod: SMS_4.... # 模板code,改为自己的值endpoint: dysmsapi.aliyuncs.com # 固定值
server:port: 8081 # 端口号
编写工具类
import com.alibaba.fastjson.JSON;
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.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import java.util.HashMap;
@Component
public class AliyunSmsUtil {@Value("${sms.keyid}")private String accessKeyId;@Value("${sms.keysecret}")private String accessKeySecret;@Value("${sms.signname}")private String signName;@Value("${sms.templatecod}")private String templateCode;@Value("${sms.endpoint}")private String endpoint;public boolean sendSms(String phoneNumber, HashMap<String, Object> templateParams) {try {DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);IAcsClient client = new DefaultAcsClient(profile);SendSmsRequest request = new SendSmsRequest();request.setPhoneNumbers(phoneNumber);request.setSignName(signName);request.setTemplateCode(templateCode);// 将HashMap转化为JSON字符串String templateParam = JSON.toJSONString(templateParams);request.setTemplateParam(templateParam);SendSmsResponse response = client.getAcsResponse(request);return "OK".equals(response.getCode());} catch (ClientException e) {e.printStackTrace();return false;}}
}
程序入口controller
import com.example.sms.AliyunSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;@RestController
@RequestMapping("/ss")
public class TsController {@Autowiredprivate AliyunSmsUtil aliyunSmsUtil;@GetMapping("/send2")public String sendCode2(String phone) {//验证码。正常业务中,这里要生成随机数,并且存到redis里,在redis里设置过期时间,如5分钟//手机号作为key,生成的验证码为valueString random = "0211";HashMap<String, Object> hashMap = new HashMap<>();hashMap.put("code", random);// 4. 调用阿里云接口发送短信,将验证码发送给指定的手机号码boolean b = aliyunSmsUtil.sendSms(phone, hashMap);System.out.println("短信发送状态:" + b);if(b){return "success";}else {return "fail";}}
}
测试:
手机也能收到验证码了。