当前位置: 首页 > news >正文

Java短信验证功能简单使用

注册登录阿里云官网:https://www.aliyun.com/

搜索短信服务
在这里插入图片描述

自己一步步申请就可以了
在这里插入图片描述

开发文档:
https://next.api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=java-tea&tab=primer-doc

1.引入依赖

<dependency><groupId>com.aliyun</groupId><artifactId>dysmsapi20170525</artifactId><version>3.1.1</version>
</dependency>

2.短信代码

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();}/*** 使用AK&amp;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);}
}
http://www.lryc.cn/news/537541.html

相关文章:

  • CAS单点登录(第7版)21.可接受的使用政策
  • 53倍性能提升!TiDB 全局索引如何优化分区表查询?
  • Pythong 解决Pycharm 运行太慢
  • 库里存储的数据有大量回车时,该如何进行存取
  • 【devops】Github Actions Secrets | 如何在Github中设置CI的Secret供CI的yaml使用
  • 体验 DeepSeek-R1:解密 1.5B、7B、8B 版本的强大性能与应用
  • 一文说清楚什么是Token以及项目中使用Token延伸的问题
  • 大模型-Tool call、检索增强
  • 【算法】【区间和】acwing算法基础 802. 区间和 【有点复杂,但思路简单】
  • Ubuntu22.04通过Docker部署Jeecgboot
  • HTML4
  • STM32F10X 启动文件完整分析
  • typescript快速入门之安装与运行
  • React源码解读
  • 【DeepSeek-R1】 API申请(火山方舟联网版)
  • 负载均衡集群——LVS-DR配置
  • 数据结构篇
  • 「软件设计模式」建造者模式(Builder)
  • Matlab 机器人 雅可比矩阵
  • DeepSeek 助力 Vue 开发:打造丝滑的面包屑导航(Breadcrumbs)
  • IntelliJ IDEA 2024.1.4版无Tomcat配置
  • chrome://version/
  • 知识图谱数据库 Neo4j in Docker笔记
  • 【动手学强化学习】02多臂老虎机
  • 【网络编程】之Udp网络通信步骤
  • Java 基于 SpringBoot+Vue 的家政服务管理平台设计与实现
  • 架构——Nginx功能、职责、原理、配置示例、应用场景
  • Spring Boot中使用Flyway进行数据库迁移
  • CAS单点登录(第7版)9.属性
  • 137,【4】 buuctf web [SCTF2019]Flag Shop