Java实现手机短信验证码
阿里云开启短信服务
进入短信服务
新建AccessKey
(不要外传)
账号:xxx
密码:xxx
后期代码授权要使用到
添加签名和模板
签名发送信息的头,一般是项目名或者公司名称,会加到短信内容的前面
模板指发送到手机上的验证码正文内容
验证码短信:签名+模板
都需要申请的:
购买套餐测试,发送效果如下:
依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.1</version> <!-- 注:如提示报错,先升级基础包版,无法解决可联系技术支持 -->
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun.mns</groupId>
<artifactId>aliyun-sdk-mns</artifactId>
<version>1.1.8</version>
</dependency>
其实这样是下载不了的,Maven没有提供,得自己手动下载jar包依赖导入项目,我已经下载好了,百度网盘提供jar包:
链接:https://pan.baidu.com/s/1QBoTDl4Zga9FD7FLHSjQxA
提取码:zxz2
把下载下来的jar包添加项目目录src下的libs,pom添加依赖:
如果报红,重启idea
<!-- aliyun sdk -->
<aliyun.sdk.version>4.1.1</aliyun.sdk.version>
<!-- aliyun sdk -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>${aliyun.sdk.version}</version>
</dependency>
<!-- 短信服务 -->
<dependency>
<groupId>sdk-core</groupId>
<artifactId>sdk-core</artifactId>
<scope>system</scope>
<version>${project.version}</version>
<systemPath>${basedir}/src/libs/aliyun-java-sdk-core-3.3.1.jar</systemPath>
</dependency>
<dependency>
<groupId>sdk-dysmsapi</groupId>
<artifactId>sdk-dysmsapi</artifactId>
<scope>system</scope>
<version>${project.version}</version>
<systemPath>${basedir}/src/libs/aliyun-java-sdk-dysmsapi-1.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>sdk-mns</groupId>
<artifactId>sdk-mns</artifactId>
<scope>system</scope>
<version>${project.version}</version>
<systemPath>${basedir}/src/libs/aliyun-sdk-mns-1.1.8.jar</systemPath>
</dependency>
如果项目需要上线,打包依赖还需要:
配置好jar路径${basedir}/src/libs
不加的话,项目maven打包成jar中的BOOT-INF/lib/(项目的所有jar依赖)将没有该本地依赖jar,这样添加的话,打包部署将带上本地目录中的jar。
</build>
<resources>
<resource>
<directory>${basedir}/src/libs</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<filtering>false</filtering>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
工具类代码
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.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import java.util.HashMap;
import java.util.Map;
/**
* Title:手机验证码工具类
* Description:阿里云短信服务
* @author WZQ
* @version 1.0.0
* @date 2020/4/18
*/
public class AliyunMessageUtil {
private static final String product = "Dysmsapi";
//产品域名,开发者无需替换
private static final String domain = "dysmsapi.aliyuncs.com";
// 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找,刚开通的AccessKey)
private static final String accessKeyId = "xxx";
private static final String accessKeySecret = "xxx";
/**
* 过程逻辑
* @param paramMap
* @return
* @throws com.aliyuncs.exceptions.ClientException
*/
public static SendSmsResponse sendSms(Map<String, String> paramMap) throws com.aliyuncs.exceptions.ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(paramMap.get("phoneNumber"));
//必填:短信签名-可在短信控制台中找到
request.setSignName(paramMap.get("msgSign"));
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(paramMap.get("templateCode"));
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(paramMap.get("jsonContent"));
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
// request.setSmsUpExtendCode(paramMap.get(“extendCode”));
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
/**
* 发送验证码短信方法,外部调用
* @param phoneNumber 接收者的手机号
* @param msgSign 阿里云签名
* @param templateCode 阿里云模板code
* @return msg
* @throws com.aliyuncs.exceptions.ClientException
*/
public static String sendMsg(String phoneNumber, String msgSign, String templateCode) throws com.aliyuncs.exceptions.ClientException{
String randomNum = createRandomNum(6);
String jsonContent = "{" + "\"code\": \"" + randomNum + "\"}";//转义字符\"
Map<String, String> paramMap = new HashMap<>();
paramMap.put("phoneNumber", phoneNumber);//接收者的手机号
paramMap.put("msgSign", msgSign);
paramMap.put("templateCode", templateCode);
paramMap.put("jsonContent", jsonContent);
SendSmsResponse sendSmsResponse = null;
sendSmsResponse = sendSms(paramMap);
if(!(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK"))) {
if(sendSmsResponse.getCode() == null) {
return "发送失败";
}
if(!sendSmsResponse.getCode().equals("错误")) {
return "发送失败";
}
}
return randomNum;//后台匹配
}
/**
生成随机数
@param num 位数
@return
*/
public static String createRandomNum(int num){
String randomNumStr = "";
for(int i = 0; i < num;i ++){
int randomNum = (int)(Math.random() * 10);
randomNumStr += randomNum;
}
return randomNumStr;
}
}
Map中参数说明
phoneNumber
:接受者手机号。
msgSign
:短信签名名称。在阿里云控制台的短信签名里能找到。
templateCode
:短信模版的code。见阿里云控制台中的模版code。
jsonContent
:需要替换的变量的JSON字符串。对于验证码来说,String jsonContent = "{" + "\"code\": " + randomNum + "}";
即可。其中randomNum是随机生成的6位验证码。
extendCode
:上行短信模板的验证码,不需要的话可以忽略
outId
:扩展字段,不需要则可以忽略
使用
对应的微服务也需要导入包和依赖,需要抛出异常
可以把验证码放到redis数据库,5分钟自动删除。
String code = AliyunMessageUtil.sendMsg(手机号,签名,模板code);
@Resource
private RedisTemplate redisTemplate;
/**
* 手机验证码
* @param phone
* @return
* @throws com.aliyuncs.exceptions.ClientException
*/
@PostMapping("/phone/code/{phone}")
public ResponseResult<Void> aliyuncode(@PathVariable("phone") String phone) throws com.aliyuncs.exceptions.ClientException {
if (!StringUtils.isEmpty(phone)){
// 格式检验,可以正则检验
if (phone.length() != 11){
return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"手机格式错误");
}
// 成功返回随机6位数字
// 注意,这里的第二三个参数需要到阿里云平台上设置的
String code = AliyunMessageUtil.sendMsg(phone,"垃圾管理信息化平台","SMS_187954123");
if (!StringUtils.isEmpty(code)){
if (code.equals("发送失败")){
return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"发送短信失败");
}
// 手机验证码的客户端标识
//Cookie cookie = new Cookie(SystemConstant.PHONE_TICKET_COOKIE, phone);
//cookie.setMaxAge(60 * 3);
//cookie.setPath(contextPath);
//response.addCookie(cookie);
// 将验证码存入Redis, 5分钟
String redisKey = RedisKeyUtil.getPhoneKey(phone);
redisTemplate.opsForValue().set(redisKey, code, 60 * 5, TimeUnit.SECONDS);
return new ResponseResult<Void>(ResponseResult.CodeStatus.OK,"发送短信成功");
}
return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"发送短信失败");
}
return new ResponseResult<Void>(ResponseResult.CodeStatus.FAIL,"参数为空");
}
/**
* 手机验证码登录
* @param loginParam
* @return
*/
@PostMapping("/phone/login")
public ResponseResult<Map<String,String>> loginByPhone(@RequestBody LoginParam loginParam){
if (StringUtils.isEmpty(loginParam.getPhone()) || StringUtils.isEmpty(loginParam.getRandString()) ){
return new ResponseResult<Map<String,String>>(ResponseResult.CodeStatus.FAIL,"请输入手机号和验证码", null);
}
//自己先验证手机号
UserInformation userInformation = userInformationService.getByPhone(loginParam.getPhone());
if(userInformation == null) {
return new ResponseResult<Map<String,String>>(ResponseResult.CodeStatus.FAIL, "该手机号未注册", null);
}
// 验证验证码
String redisKey = RedisKeyUtil.getPhoneKey(loginDto.getPhone());
String code = (String) redisTemplate.opsForValue().get(redisKey);
if (StringUtils.isEmpty(code) || !code.equalsIgnoreCase(loginDto.getCode())) {
return new ResponseResult<Map<String,String>>(ResponseResult.CodeStatus.FAIL,"验证码错误或验证码失效", null);
}
Map<String, String> token = new HashMap<>();
Map<String, String> subject = new HashMap<>();
subject.put("phone",loginParam.getPhone());
token.put("token", JwtUtil.createJWT(IdUtil.simpleUUID(), JSON.toJSONString(subject),null));
return new ResponseResult<Map<String,String>>(ResponseResult.CodeStatus.OK, "登录成功", token);
}