北京网站优化网,如何做视频类网站,网站seo入门,河北廊坊做网站注册登录阿里云官网#xff1a;https://www.aliyun.com/
搜索短信服务
自己一步步申请就可以了
开发文档#xff1a; https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version2017-05-25languagejava-teatabprimer-doc
1.引入依赖
dependency…注册登录阿里云官网https://www.aliyun.com/
搜索短信服务
自己一步步申请就可以了
开发文档 https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version2017-05-25languagejava-teatabprimer-doc
1.引入依赖
dependencygroupIdcom.aliyun/groupIdartifactIddysmsapi20170525/artifactIdversion3.1.1/version
/dependency2.短信代码
package com.mc.sms.utils;import com.aliyun.tea.*;
import java.util.Random;/*** 此处有两个方法* 1.生成随机六位数的验证码* 2.发送验证码* author 灯塔* date: 2024/09/26 18:19* version 1.0.0*/
public class SendCodeUtils {private static String ALIBABA_CLOUD_ACCESS_KEY_ID 你的AccessKey;private static String ALIBABA_CLOUD_ACCESS_KEY_SECRET 你的 AccessKey Secret;private static String SignName 签名名称;private static String TemplateCode 模板code;/*** 生成随机的六位验证码* return String*/public static String generateVerificationCode() {// 设置验证码长度为6int length 6;// 验证码字符集String digits 0123456789;Random random new Random();StringBuilder sb new StringBuilder();// 生成六位数验证码for (int i 0; i length; i) {int index random.nextInt(digits.length());sb.append(digits.charAt(index));}return sb.toString();}/*** 使用AKamp;SK初始化账号Client* return* type com.aliyun.dysmsapi20170525.Client* throws Exception*/public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {com.aliyun.teaopenapi.models.Config config new com.aliyun.teaopenapi.models.Config().setAccessKeyId(ALIBABA_CLOUD_ACCESS_KEY_ID).setAccessKeySecret(ALIBABA_CLOUD_ACCESS_KEY_SECRET);config.endpoint dysmsapi.aliyuncs.com;return new com.aliyun.dysmsapi20170525.Client(config);}/**** param phoneNumber* param verificationCode* return String* throws Exception*/public static String verify(String phoneNumber, String verificationCode) throws Exception {com.aliyun.dysmsapi20170525.Client client SendCodeUtils.createClient();com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(phoneNumber).setSignName(SignName).setTemplateCode(TemplateCode).setTemplateParam({\code\:\ verificationCode \});try {// 复制代码运行请自行打印 API 的返回值client.sendSmsWithOptions(sendSmsRequest, new com.aliyun.teautil.models.RuntimeOptions());return verificationCode;} catch (TeaException error) {// 此处仅做打印展示请谨慎对待异常处理在工程项目中切勿直接忽略异常。// 错误 messageSystem.out.println(error.getMessage());// 诊断地址System.out.println(error.getData().get(Recommend));com.aliyun.teautil.Common.assertAsString(error.message);return null;} catch (Exception _error) {TeaException error new TeaException(_error.getMessage(), _error);// 此处仅做打印展示请谨慎对待异常处理在工程项目中切勿直接忽略异常。// 错误 messageSystem.out.println(error.getMessage());// 诊断地址System.out.println(error.getData().get(Recommend));com.aliyun.teautil.Common.assertAsString(error.message);return null;}}
}3.测试
RestController
RequestMapping(/sms)
public class SmsController {RequestMapping(/sendSms)public R SendSMS() throws Exception {String code SendCodeUtils.generateVerificationCode();String phoneNumber 18618372228;String verify SendCodeUtils.verify(phoneNumber, code);System.out.println(verify);return R.ok().data(verify);}
}