短信发送
介绍
目前市面上有很多第三方提供的短信服务,这些第三可方短信服务会和各个运营商(移动、联通、电信)对接,我们只需要注册成为会员并且按照提供的开发文档进行调用就以发送短信。需要说明的是,这些短信服务一般都是收费服务。
常用短信服务
- 阿里云
- 华为云
- 腾讯云
- 京东
- 梦网
- 乐信
阿里云短信服务(Short Message Service)是广大企业客户快速触达手机用户所优选使用的通信能力。调用API或用群发助手,即可发送验证码、通知类和营销类短信:国内验证短信秒级触达,到达率最高可达99%;国际/港澳台短信覆盖200多个国家和地区,安全稳定,广受出海企业选用。
阿里云短信服务
注册账号: 登录阿里云官网 阿里云-计算,为了无法计算的价值
申请短信签名: 选择国内消息菜单, 短信签名是短信发送者的署名,表示发送方的身份
- 个人不具备申请签名的条件
设置短信模版: 短信模板包含短信发送内容、场景、变量信息
设置AccessKey: 用于程序中的身份鉴权
- 点击【AccessKey管理】
- 使用子账户, 因为AcessKey具有全部权限, 一旦泄漏风险很大
- 创建子用户
- AccessKey就是用户名, AccessKey Secret就是密码, 程序中会使用到
- 设置权限
- 禁用用户
代码开发
使用阿里云短信服务发送短信,可以参照官方提供的文档即可,
1、导入maven坐标
com.aliyun
aliyun-java-sdk-core
4.5.16
com.aliyun
aliyun-java-sdk-dysmsapi
2.1.0
2、调用API
package com.itheima.reggie.utils;
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;
/**
* 短信发送工具类
*/
public class SMSUtils {
/**
* 发送短信
* @param signName 签名
* @param templateCode 模板
* @param phoneNumbers 手机号
* @param param 参数
*/
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
request.setPhoneNumbers(phoneNumbers);
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setTemplateParam("{\"code\":\""+param+"\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
}catch (ClientException e) {
e.printStackTrace();
}
}
}
验证码登录
需求分析
为了方便用户登录,移动端通常都会提供通过手机验证码登录的功能
- 手机验证码登录的优点:
- 方便快捷,无需注册,直接登录
- 使用短信验证码作为登录凭证,无需记忆密码
- 安全
- 登录流程
- 输入手机号>获取验证码>输入验证码>点击登录>登录成功
- 注意:通过手机验证码登录,手机号是区分不同用户的标识。
通过手机验证码登录时,涉及的表为user表,即用户表。
在开发代码之前,梳理登录时前端页面和服务端的交互过程:
- 在登录页面(front/page/login,html)输入手机号,点击【获取验证码】按钮,页面发送ajax请求,在服务端调用短信服务API给指定手机号发送验证码短信
- 在登录页面输入验证码,点击【登录】按钮,发送ajax请求,在服务端处理登录请求
- 开发手机验证码登录功能,其实就是在服务端编写代码去处理前端页面发送的这2次请求即可,
获取验证码
在开发业务功能前,先将需要用到的类和接口基本结构创建好
/**
* 随机生成验证码工具类
*/
public class ValidateCodeUtils {
/**
* 随机生成验证码
* @param length 长度为4位或者6位
* @return
*/
public static Integer generateValidateCode(int length){
Integer code =null;
if(length == 4){
code = new Random().nextInt(9999);//生成随机数,最大为9999
if(code < 1000){
code = code + 1000;//保证随机数为4位数字
}
}else if(length == 6){
code = new Random().nextInt(999999);//生成随机数,最大为999999
if(code < 100000){
code = code + 100000;//保证随机数为6位数字
}
}else{
throw new RuntimeException("只能生成4位或6位数字验证码");
}
return code;
}
/**
* 随机生成指定长度字符串验证码
* @param length 长度
* @return
*/
public static String generateValidateCode4String(int length){
Random rdm = new Random();
String hash1 = Integer.toHexString(rdm.nextInt());
String capstr = hash1.substring(0, length);
return capstr;
}
}
/**
* 用户信息
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
//姓名
private String name;
//手机号
private String phone;
//性别 0 女 1 男
private String sex;
//身份证号
private String idNumber;
//头像
private String avatar;
//状态 0:禁用,1:正常
private Integer status;
}
@Mapper
public interface UserMapper extends BaseMapper {
}
public interface UserService extends IService {
}
@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
}
/**
* 移动端用户管理
*/
@RestController
@RequestMapping("/")
@Slf4j
public class UserController {
}
在全局过滤器中扩展逻辑,判断移动端用户登录状态, 对于获取手机验证码和登录, 需要直接放行。
/**
* 过滤器: 检测用户是否登录
* 拦截的路径: urlPatterns = "/*"
*/
@WebFilter(filterName = "loginCheckFilter", urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//定义不需要处理的请求路径
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**",
"/common/**",
"/user/sendMsg", //移动端发送短信
"/user/login" //移动端登录
};
... ...
//4-2.判断登录状态, 已登录放行, 移动端
if(request.getSession().getAttribute("user") != null) {
//把当前登录用户的id存入ThreadLocal
long userId = (long) request.getSession().getAttribute("user");
BaseContext.setCurrentId(userId);
filterChain.doFilter(request, response);
return;
}
}
}
获取手机验证码接口
/**
* 移动端用户管理
*/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserService userService;
/**
* 发送手机验证码
* @param user
* @return
*/
@PostMapping("/sendMsg")
public R sendMsg(@RequestBody User user, HttpSession session){
//获取手机号
String phone = user.getPhone();
if(phone != null){
//生成随机的4位验证码
String code = ValidateCodeUtils.generateValidateCode(4).toString();
log.info("code={}",code);
//调用阿里云提供的短信服务API完成发送短信
//SMSUtils.sendMessage("瑞吉外卖", "", phone, code);
//需要将生成的验证码保存到Session
session.setAttribute(phone,code);
return R.success("手机验证码短信发送成功");
}
return R.error("短信发送失败");
}
}
用户登录
/**
* 移动端用户管理
*/
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserService userService;
/**
* 移动端用户登录
* @param map
* @return
*/
@PostMapping("/login")
public R login(@RequestBody Map map, HttpSession session) {
log.info(map.toString());
//获取手机号
String phone = (String) map.get("phone");
//获取用户输入的验证码
String code1 = (String) map.get("code");
//从Seeison中获取保存的验证码
String code2 = (String) session.getAttribute(phone);
//验证码对比, 页面提交的验证码和Session中保存的验证码对比
if (code2 != null && code2.equals(code1)) {
//登录成功
//判断当前用户是否为新用户, 如果是新用户就自动完成注册
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getPhone, phone);
User user = userService.getOne(queryWrapper);
if (user == null) {
user = new User();
user.setPhone(phone);
user.setStatus(1);
userService.save(user);
}
session.setAttribute("user", user.getId());
return R.success(user);
}
return R.error("登录失败");
}
}
- 访问: http://localhost:8080/front/page/login.html
- 注意浏览器用手机模式调试