短信验证码
程序员文章站
2022-05-13 16:01:55
...
pom.xml必备依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.0.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
serviceImpl
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.cqf.sendsms.service.SendSms;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class SendSmsImpl implements SendSms {
@Override
public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
//连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<secret>");
IAcsClient client = new DefaultAcsClient(profile);
//构建请求
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
request.setDomain("dysmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SendSms");
// 自定义的参数 (手机号,验证码,签名,模板)
request.putQueryParameter("PhoneNumbers", phoneNum);
request.putQueryParameter("SignName", "科技创新工场");
request.putQueryParameter("TemplateCode", templateCode);
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
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;
}
}
controller
import com.aliyuncs.utils.StringUtils;
import com.cqf.sendsms.service.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@CrossOrigin //跨域支持
public class SmsApiController {
@Autowired
private SendSms sendSms;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping("/send/{phone}")
public String code(@PathVariable("phone") String phone) {
// 调用发送方法 (模拟真实业务 redis)
String code = redisTemplate.opsForValue().get(phone);
if (!StringUtils.isEmpty(code)) {
return phone + ":" + code + "已存在,还没有过期";
}
//生成验证码并存储到redis中
code = UUID.randomUUID().toString().substring(0, 4);
HashMap<String, Object> param = new HashMap<>(16);
param.put("code", code);
boolean isSend = sendSms.send(phone, "<templateCode>", param);
if (isSend) {
redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.SECONDS);
return phone + ":" + code + "发送成功";
} else {
return "发送失败";
}
}
}