阿里云使用短信服务
将阿里云SMS配置好后。
可以参考官方文档https://help.aliyun.com/product/44282.html?spm=5176.12212999.0.0.1bf31cbeHR8kYJ
导入相关依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.71</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
在test中执行如下代码:
String phone = "17673842485"; String SignName = "大拇指"; String TemplateCode = "SMS_200690536"; Map<String, Object> TemplateParam = new HashMap<>(); // 连接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "签名", "密码"); 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", phone); // 签名 request.putQueryParameter("SignName", SignName); // 模板code request.putQueryParameter("TemplateCode", TemplateCode); // 参数 TemplateParam.put("code", 12345); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(TemplateParam)); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); }
发送成功提示
在结合noSql使用,作者使用的是redis
#以下都是默认的,可添加自己的配置,启动redis即可。
spring.redis.port=6379 spring.redis.host=127.0.0.1
service目录代码
@Service public class SendSmsService implements SendSmsImpl { @Override public boolean send(String phone, String templateCode, Map<String, Object> code) { String SignName = "大拇指"; // 连接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", ""); 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", phone); // 签名 request.putQueryParameter("SignName", SignName); // 模板code 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目录代码
@RestController @CrossOrigin // 跨域 (如已在config中配置了cros则不需要添加该注释) public class SendSmsController { @Resource private SendSmsImpl sendSms; @Autowired private RedisTemplate<String, String> redisTemplate; @GetMapping("/sendSms/{phone}") public String sendSms(@PathVariable("phone") String phone){ String code = redisTemplate.opsForValue().get(phone); if (!StringUtils.isEmpty(code)){ return code +"还没有过期,能够继续使用"; } HashMap<String, Object> map = new HashMap<>(); String coded = UUID.randomUUID().toString().substring(0, 6); map.put("code", coded); boolean send = sendSms.send(phone, "SMS_200690536", map); if (send){ return "成功"; } return "验证失败!"; } }
版权声明:本文为yiMro原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。