长沙做网站报价,学院网站群建设的目标,哪些公司的网站做的很好,网站后台管理怎么做友情链接详细介绍SpringBoot整合阿里云短信服务的每一步过程#xff0c;同时会将验证码存放到Redis中并设置过期时间#xff0c;尽量保证实战的同时也让没做过的好兄弟也能实现发短信的功能~
1. 注册阿里云账号和创建Access Key 首先#xff0c;你需要注册一个阿里云账号#xff0…详细介绍SpringBoot整合阿里云短信服务的每一步过程同时会将验证码存放到Redis中并设置过期时间尽量保证实战的同时也让没做过的好兄弟也能实现发短信的功能~
1. 注册阿里云账号和创建Access Key 首先你需要注册一个阿里云账号如果还没有然后在控制台中创建Access Key。这个Access Key将用于通过API调用阿里云短信服务。在控制台中创建Access Key非常简单只需遵循阿里云的步骤即可。
2. 添加相关的依赖 在Spring Boot项目中你需要添加阿里云短信服务、Redis的依赖、还有mybatis-plus
这里直接用mybatis-plus了非常的方便省去了大量的DOM操作你可以在pom.xml文件中添加以下依赖 dependencygroupIdcom.baomidou/groupIdartifactIdmybatis-plus-boot-starter/artifactIdversion3.5.7/version/dependencydependencygroupIdcom.aliyun/groupIdartifactIddysmsapi20170525/artifactIdversion3.0.0/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependency
3. 配置阿里云短信服务、Redis参数
application.yml:
# 服务器配置
server:# 设置服务器端口port: 8080# 配置Servlet的上下文路径servlet:context-path: /api# 数据源配置用于连接MySQL数据库
spring:datasource:# 数据库驱动类名driver-class-name: com.mysql.cj.jdbc.Driver# 数据库连接URLurl: jdbc:mysql://localhost:3306/×××# 数据库用户名username: ×××# 数据库密码password:×××# Redis配置redis:# Redis服务器地址host: ×××# Redis服务器端口port: ×××# Redis数据库索引database: ×××# MyBatis-Plus配置
mybatis-plus:# 全局配置global-config:# 数据库配置db-config:# 表名前缀table-prefix: ×××# 主键类型自动根据数据库生成id-type: auto# 映射器位置指定mapper接口的XML文件位置mapper-locations: classpath*:mapper/*.xml# MyBatis配置configuration:# 日志实现类使用控制台输出日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl配置Redis Configuration
public class RedisConfig {Beanpublic RedisTemplateString,Object redisTemplate(RedisConnectionFactory factory){RedisTemplateString, Object template new RedisTemplate();StringRedisSerializer redisSerializer new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);// key序列化template.setKeySerializer(redisSerializer);// value序列化template.setValueSerializer(jackson2JsonRedisSerializer);// value hashmap序列化 filed valuetemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.setHashKeySerializer(redisSerializer);return template;}
}4. 创建工具类MsgController
阿里云短信服务Utils
public class SendMsgUtil {/*** 使用AKSK初始化账号Client* return Client* throws Exception*/public static Client createClient() throws Exception {com.aliyun.teaopenapi.models.Config config new com.aliyun.teaopenapi.models.Config()// 必填请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。.setAccessKeyId(xxxxxxxxxx)// 必填请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。.setAccessKeySecret(xxxxxxxx);config.endpoint dysmsapi.aliyuncs.com;return new Client(config);}/*** API 相关* return OpenApi.Params*/public static com.aliyun.teaopenapi.models.Params createApiInfo() throws Exception {com.aliyun.teaopenapi.models.Params params new com.aliyun.teaopenapi.models.Params().setAction(SendSms).setVersion(2017-05-25).setProtocol(HTTPS).setMethod(POST).setAuthType(AK).setStyle(RPC).setPathname(/).setReqBodyType(json).setBodyType(json);return params;}public static String sendCode(String phone) throws Exception {Client client createClient();com.aliyun.teaopenapi.models.Params params createApiInfo();java.util.MapString, Object queries new java.util.HashMap();queries.put(PhoneNumbers, xxxx);queries.put(SignName, xxxx);queries.put(TemplateCode, xxxxx); //您正在申请手机注册验证码为${code}5分钟内有效String code generateVerificationCode();queries.put(TemplateParam, {\code\:\ code \});com.aliyun.teautil.models.RuntimeOptions runtime new com.aliyun.teautil.models.RuntimeOptions();com.aliyun.teaopenapi.models.OpenApiRequest request new com.aliyun.teaopenapi.models.OpenApiRequest().setQuery(com.aliyun.openapiutil.Client.query(queries));client.callApi(params, request, runtime);return code;}// 生成六位数的验证码public static String generateVerificationCode() {Random random new Random();int firstDigit random.nextInt(9) 1;StringBuilder sb new StringBuilder().append(firstDigit);for (int i 0; i 5; i) {sb.append(random.nextInt(10));}return sb.toString();}}
像service层和mapper层以及entity层 都可以直接用mybatis-plus生成 5. 短信验证码实现登录注册
接下来直接测试我是在idea里使用插件Apipost进行测试的也非常的好用: 点击发送后输入的手机号就会收到一个验证码来进行验证 然后点击登陆测试 结果 还有一个注册功能大概流程跟这一样只不过会判断手机号是不是被注册过接下来跟大家聊聊为什么要把验证码存储在redis当中 将验证码保存到 Redis 中的优势在于其高性能、分布式特性、易设置过期时间、减少数据库负担和提升安全性。Redis 提供快速读写和自动过期管理确保高效响应和验证码的及时失效从而提高系统整体性能和安全性。