前期准备redis,springboot,阿里云短信服务
新建一个springboot项目
1.导入依赖
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
2.创建service
import java.util.Map;
public interface SendSms {
public boolean send(String phoneNum, String TemplateCode, Map<String,Object> code);
}
3.创建实现类
package com.lj.sms.service;
import com.alibaba.fastjson.JSON;
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 org.springframework.stereotype.Service;
import java.util.HashMap;
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", "accessKey", "密码");
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("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", phoneNum);
request.putQueryParameter("SignName", "java学习网站");
request.putQueryParameter("TemplateCode", TemplateCode);
HashMap<String, Object> map = new HashMap<>();
// map.put("code", 2233);
request.putQueryParameter("TemplateParam", JSON.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;
}
}
4.创建控制层
package com.lj.sms.controller;
import com.aliyuncs.utils.StringUtils;
import com.lj.sms.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 SendSmsController {
@Autowired
private SendSms sendSms;
@Autowired
private RedisTemplate<String,String> redisTemplatel;
@GetMapping("/send/{phone}")
public String code(@PathVariable("phone") String phone){
String code = redisTemplatel.opsForValue().get(phone);
if(!StringUtils.isEmpty(code)) {
return phone + ":" + code + "已存在还没过期";
}
code = UUID.randomUUID().toString().substring(0,4);
HashMap<String,Object> param = new HashMap<>();
param.put("code",code);
boolean isSend = sendSms.send(phone,"SMS_189616774",param);
if(isSend){
redisTemplatel.opsForValue().set(phone,code,5, TimeUnit.SECONDS);
return phone + ": " +code + "发送成功!";
}else {
return "发送失败!";
}
}
}
5.配置文件
server.port=9899
server.redis.host=127.0.0.1
server.redis.port=6379