一、在模块的pom中引入依赖
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
二、编写controller,根据手机号发送短信
package com.jzd.msmService.controller;
import com.jzd.commonUtils.Result;
import com.jzd.msmService.service.MsmService;
import com.jzd.msmService.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @Author jzd
* @Date 2020/8/8 9:06
*/
@CrossOrigin
@RestController
@RequestMapping("/msmService")
public class MsmController {
@Autowired
private MsmService msmService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
//短信发送
@GetMapping("/send/{phone}")
public Result sendMsm(@PathVariable String phone){
//从Redis获取验证码
String code = redisTemplate.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)){
return Result.ok();
}
code = RandomUtil.getSixBitRandom();
Map<String,Object> param = new HashMap<>();
param.put("code",code);
boolean isSend = msmService.send(param,phone);
if (isSend){
//发送成功,存到Redis
//设置验证码有效时间
redisTemplate.opsForValue().set(phone, code,5, TimeUnit.MINUTES);
return Result.ok();
}else {
return Result.error().message("短信发送失败");
}
}
}
三、编写service
package com.jzd.msmService.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.jzd.msmService.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Map;
/**
* @Author jzd
* @Date 2020/8/8 9:07
*/
@Service
public class MsmServiceImpl implements MsmService {
@Override
public boolean send(Map<String, Object> param, String phone) {
if(StringUtils.isEmpty(phone)) return false;
DefaultProfile profile =
DefaultProfile.getProfile("default", "id号", "秘钥");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
//request.setProtocol(ProtocolType.HTTPS);
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", "签名名称");
request.putQueryParameter("TemplateCode", "模板码");
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
return response.getHttpResponse().isSuccess();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
四、总结