个人用户实现发送短信功能
准备工作
因为国家政策 ,阿里云和其他的大型短信调用网站审核严格所以我们用比较普通的榛子云来练习短信验证
了解阿里云短信码
登录阿里云,选择短信服务
首先要了解发送短信的四个步骤
进入申请签名,查看大致内容,可以看到这边需要用到很多证,点击下载委托书模板查看证件的大致内容。
下载完毕后可以看到,委托书大概内容如下:
申请签名之后需要在后台验证这个签名,由于我们申请不了,这边只做了解即可。其余的三个步骤点进去查看大概内容,在面试的时候能够说出一二即可。
榛子云短信系统
选择注册榛子云短信用户注册系统
注册之后进入如下页面
注意:榛子云只是一个测试系统,真实开发中还是使用阿里云短信服务,这边的榛子云发送短信需要先进行充值。
打开我的应用,查看这边的APPId,AppSecret,这边后续开发中会用到。
创建项目
创建一个SpringBoot项目,这边最好选择自定义的阿里云网址,防止创建项目失败的情况。
导入如下依赖
<dependency><groupId>com.zhenzikj</groupId><artifactId>zhenzisms</artifactId><version>2.0.2</version></dependency>
创建完毕之后,使用阿里域名创建的项目会自动生成一个properties文件,只需要改动数据库连接地址,用户名和密码即可。
复制MessageDemo
package com.zb.utils;import com.zhenzi.sms.ZhenziSmsClient;import java.util.HashMap;
import java.util.Map;
import java.util.Random;public class MessageDemo {public static Map<String,Object> sendMessage(String phone){Map<String,Object> map = new HashMap<>();String apiUrl="https://sms_developer.zhenzikj.com";String appId="xxxx";//在控制台的 我的应用String appSecret="xxxx";//在控制台的 我的应用ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);Map<String, Object> params = new HashMap<String, Object>();params.put("number", phone);//你要发送的手机号params.put("templateId", "10226");//短信模板里的String[] templateParams = new String[2];//模板int i = new Random().nextInt(9999 - 1000 + 1);int sjs=i+1000;String yzm = String.valueOf(sjs);templateParams[0] = yzm;templateParams[1] = "5分钟";params.put("templateParams", templateParams);String result = null;try {result = client.send(params);} catch (Exception e) {e.printStackTrace();}map.put("yzm",yzm);map.put("result",result);System.out.println(result);return map;}
}
粘贴自己应用的几个id
因为需要验签,所以验证码应该放在redis中,所以应该写一个redis的工具类。
package com.zb.utils;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);ObjectMapper objectMapper = new ObjectMapper();objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);serializer.setObjectMapper(objectMapper);RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();redisTemplate.setConnectionFactory(redisConnectionFactory);redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(serializer);redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(serializer);redisTemplate.afterPropertiesSet();return redisTemplate;}@Beanpublic RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration);}
}
写一个简单的登录页面
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>验证码登录</title><script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script></head>
<body>手机号:<input id="phone"/><br/>验证码:<input id="code"/><br/>
<button id="getCode">获取验证码</button>
<button id="login">登录</button></body><script>login();function login(){$("#getCode").click(function(){var phone = $("#phone").val();$.ajax({url:"/sendMessage",data:{'phone':phone},datatype:"json",type:"post",success:function(msg){alert(msg);if(msg=='{"code":0,"data":"发送成功"}'){alert("发送成功");}}})})$("#login").click(function () {var code = $("#code").val();$.ajax({url:"/verify",type:"post",data:{"code":code},dataType:"json",success:function (msg) {if(msg==true){alert("登录成功");}else{alert("登录失败")}},error:function () {alert("网络正忙");}})})}</script>
</html>
最后写一个controller控制器进行跳转,判断验证码以及发送短信的操作。
package com.zb.controller;import com.zb.utils.MessageDemo;
import com.zb.utils.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Map;
import java.util.concurrent.TimeUnit;@Controller
public class UserController {@Autowiredprivate RedisConnectionFactory redisConnectionFactory;@Autowired(required = false)private RedisConfig redisConfig;/*** 登录* @return*/@RequestMapping("toLogin")public String toLogin(){return "login";}/*** 发送信息* @return*/@RequestMapping("sendMessage")@ResponseBodypublic String sendMessage(@RequestParam String phone){System.out.println(phone);Map<String,Object> map = MessageDemo.sendMessage(phone);String yzm = (String) map.get("yzm");RedisTemplate<String, Object> redisTemplate = redisConfig.redisTemplate(redisConnectionFactory);redisTemplate.opsForValue().set("yzm",yzm,3000, TimeUnit.SECONDS);String result = (String) map.get("result");return result;}/*** 判断验证码*/@RequestMapping("/verify")@ResponseBodypublic Boolean verify(String code){RedisTemplate<String, Object> redisTemplate = redisConfig.redisTemplate(redisConnectionFactory);String yzm = (String) redisTemplate.opsForValue().get("yzm");Boolean flag = false;if(yzm.equals(code)){flag = true;}return flag;}}
操作页面如下:
到这边基本就完成了
个人用户实现发送短信功能相关推荐
- 【短信发送】实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现
实现腾讯云发送短信功能--工具类和SpringBoot配置两种方法实现 一.开通腾讯云短信服务 二.工具类--使用qcloudsms实现短信发送 三.Spring Boot项目引入短信发送功能 我们发 ...
- TP5调用阿里云短信接口实现发送短信功能
TP5调用阿里云短信接口实现发送短信功能 工作需要,对接阿里云的短信服务,借此记录一下(网站 www.lelee.top) 主要分为两个部分,一是下载官方的Demo事例,这个不需要多说,自己去下载就行 ...
- 实现 Java 发送短信功能
前言 前几日,有粉丝后台私信我如何实现发送短信的功能,刚好久一刚刚用到了这个功能,就把具体的实现过程记录下来,分享给大家. 我们日常使用的软件或者网站,大部分都在使用短信业务,比如注册.验证码功能.还 ...
- springboot集成阿里云短信服务,实现发送短信功能
springboot集成阿里云短信服务,实现发送短信功能 准备工作: 1.登陆阿里云->进入控制台->开通短信服务(进入后根据提示开通) 2.充值(借人家平台发短信你以为不要钱的?我充了3 ...
- 7 分钟实现 Java 发送短信功能!
阅读文本大概需要 7 分钟. 前言 前几日,有粉丝后台私信我如何实现发送短信的功能,刚好久一刚刚用到了这个功能,就把具体的实现过程记录下来,分享给大家. 我们日常使用的软件或者网站,大部分都在使用短信 ...
- php可以发短信的代码,PHP代码函数实现PHP发送短信功能
//PHP代码函数实现PHP发送短信功能 //本代码基于开心洋葱 运行 //代码来源:开心洋葱 //PHP发送短信开心洋葱专用(PHP代码函数) //本代码基于开心洋葱 运行 //代码来源:开心洋葱 ...
- android 发短信意图,android代码实现打电话和发送短信功能
android代码实现打电话和发送短信功能 发布时间:2020-05-27 11:57:17 来源:亿速云 阅读:172 作者:鸽子 1.XML布局 xmlns:app="http://sc ...
- 个人永久性免费-Excel催化剂功能第85波-灵活便捷的批量发送短信功能(使用腾讯云接口)...
微信时代的今天,短信一样不可缺席,大系统都有集成短信接口. 若只是临时用一下,若能够直接在Excel上加工好内容就可以直接发送,这些假设在此篇批量群发短信功能中都为大家带来完美答案. 业务场景 不多说 ...
- MTK 7.1 定时发送短信功能
此功能的主要设计思路是: 在短信添加附件的list列表中添加一个定时发送短信的选项,用户可以通过添加定时短信来设置短信要发送出去的时间,定时短信发送出去后,后台定时器会根据用户设置的时间进行定时判定, ...
- TP5 封装多业务的发送短信功能(包括国际短信)
1.准备工作 1.1.准备依赖包 这个网站提供的都是PHP包,挺有用的 https://packagist.org/ 另外还可以在GitHub上面下载 https://github.com/ 1.短信 ...
最新文章
- 深入理解 SpringBoot 启动机制(starter 机制)
- android Json详解
- ZeptoN正在将程序放入Java
- C# - 类_使用新成员隐藏基类成员
- C——printf和scanf返回值问题
- AD域首次登陆修改密码设置
- IEEE JBHI 投稿因格式问题打回记录
- [转]软件开发的“三重门”
- E - 确定比赛名次(拓补排序)
- 数据结构和算法 数论 素数/质数、回文素数
- 评测|HPE Nimble AF全闪存系列,诠释真正的高端存储
- 数学建模学习(47):灰色预测模型案例一(代码模板+模型建立)
- L1-6 烤地瓜 (15 分)
- 去哪儿APP 算法初探窥镜
- 【Stream】java8新特性Stream流总结
- Word如何绘制整行下划线,如何给分好的栏添加分隔线?
- Oracle新建的用户看不到表,oracle中用命令行新建的用户没法建表
- python基础:廖雪峰的官方网站Python(部分)笔记
- MessageBox 按钮显示英文或其他语言
- 海盗分金c语言算法,[经典算法]海盗分金问题sql求解(贪心算法)