目录
1 云之讯短信验证码
短信验证码选用云之讯第三方短信平台:https://www.ucpaas.com/
选择原因:注册赠送10元,无实名认证也可以发验证码短信。现在也没了3元好像
1.1 创建应用
自行注册、登录后进行创建应用:
默认情况下,短信只能发送给自己注册的手机号,为了测试方便需要添加测试手机号(最多6个):
1.2 创建短信模板
发送短信需要创建短信模板,模板中采用{1}、{2}的形式作为参数占位。
模板处于待审核状态是不可以使用的,需要审核通过后才能使用,审核通过后会有短信通知。
1.3 发送短信api
地址:http://docs.ucpaas.com/doku.php?id=%E7%9F%AD%E4%BF%A1:sendsms
1.4 编写代码
配置RestTemplateConfig:
package com.oldlu.sso.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.converter.StringHttpMessageConverter;
import java.nio.charset.Charset;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
// 支持中文编码
restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setReadTimeout(5000);//单位为ms
factory.setConnectTimeout(5000);//单位为ms
return factory;
}
}
编写SmsService:
package com.oldlu.sso.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class SmsService {
@Autowired
private RestTemplate restTemplate;
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 发送验证码短信
*
* @param mobile
*/
public String sendSms(String mobile) {
String url = "https://open.ucpaas.com/ol/sms/sendsms";
Map<String, Object> params = new HashMap<>();
params.put("sid", "*******");
params.put("token", "*******");
params.put("appid", "*******");
params.put("templateid", "*****");
params.put("mobile", mobile);
// 生成4位数验证 如果是多个参数用,分隔
params.put("param", RandomUtils.nextInt(100000, 999999));
ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, params, String.class);
String body = responseEntity.getBody();
try {
JsonNode jsonNode = MAPPER.readTree(body);
//000000 表示发送成功
if (StringUtils.equals(jsonNode.get("code").textValue(), "000000")) {
return String.valueOf(params.get("param"));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
1.5 编写测试用例
package com.oldlu.sso.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSmsService {
@Autowired
private SmsService smsService;
@Test
public void sendSms(){
this.smsService.sendSms("158****7944");
}
}
1.6 编写接口服务
编写ErrorResult:
package com.oldlu.sso.vo;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class ErrorResult {
private String errCode;
private String errMessage;
}
SmsController:
package com.oldlu.sso.controller;
import com.oldlu.sso.service.SmsService;
import com.oldlu.sso.vo.ErrorResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("user")
public class SmsController {
private static final Logger LOGGER = LoggerFactory.getLogger(SmsController.class);
@Autowired
private SmsService smsService;
/**
* 发送验证码
*
* @return
*/
@PostMapping("login")
public ResponseEntity<Object> sendCheckCode(@RequestBody Map<String, Object> param) {
ErrorResult.ErrorResultBuilder resultBuilder = ErrorResult.builder().errCode("000000").errMessage("发送短信验证码失败");
try {
String phone = String.valueOf(param.get("phone"));
Map<String, Object> sendCheckCode = this.smsService.sendCheckCode(phone);
int code = ((Integer) sendCheckCode.get("code")).intValue();
if (code == 3) {
return ResponseEntity.ok(null);
}else if(code == 1){
resultBuilder.errCode("000001").errMessage(sendCheckCode.get("msg").toString());
}
} catch (Exception e) {
LOGGER.error("发送短信验证码失败", e);
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultBuilder.build());
}
}
SmsService:
package com.oldlu.sso.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
@Service
public class SmsService {
private static final Logger LOGGER = LoggerFactory.getLogger(SmsService.class);
@Autowired
private RestTemplate restTemplate;
private static final ObjectMapper MAPPER = new ObjectMapper();
@Autowired
private RedisTemplate<String, String> redisTemplate;
/**
* 发送验证码
*
* @param mobile
* @return
*/
public Map<String, Object> sendCheckCode(String mobile) {
Map<String, Object> result = new HashMap<>(2);
try {
String redisKey = "CHECK_CODE_" + mobile;
String value = this.redisTemplate.opsForValue().get(redisKey);
if (StringUtils.isNotEmpty(value)) {
result.put("code", 1);
result.put("msg", "上一次发送的验证码还未失效");
return result;
}
String code = this.sendSms(mobile);
if (null == code) {
result.put("code", 2);
result.put("msg", "发送短信验证码失败");
return result;
}
//发送验证码成功
result.put("code", 3);
result.put("msg", "ok");
//将验证码存储到Redis,2分钟后失效
this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(2));
return result;
} catch (Exception e) {
LOGGER.error("发送验证码出错!" + mobile, e);
result.put("code", 4);
result.put("msg", "发送验证码出现异常");
return result;
}
}
}
package com.itheima.sso.vo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result {
private boolean success;
private int code;
private String message;
}
public Result sendCheckCode(String phone) {
String checkCodeKEY = "CHECK_CODE_" + phone;
String redisCode = redisTemplate.opsForValue().get(checkCodeKEY);
if (StringUtils.isNotEmpty(redisCode)){
return new Result(true,1,"上一次发送的验证码还未失效");
}
String code = this.sendSms(phone);
if (code == null){
return new Result(false,2,"发送验证码失效");
}
//存储在redis中
this.redisTemplate.opsForValue().set(checkCodeKEY,code, Duration.ofHours(24));
return new Result(true,3,"发送验证码成功");
}
2 阿里云短信服务
云之讯平台如果不能审核通过的话,可以使用阿里云短信服务进行发送。
2.1 申请签名与模板
https://dysms.console.aliyun.com/dysms.htm?spm=5176.12818093.0.ddysms.2a4316d0ql6PyD
说明:申请签名时,个人用户只能申请一个并且签名的名称必须为“ABC商城”,否则审核不通过。
申请模板
审核时间需要12小时,请耐心等待
2.2 设置用户权限
在阿里云中,需要在RAM服务中创建用户以及权限,才能通过api进行访问接口。
创建用户
创建完成后要保存AccessKey Secret和AccessKey ID,AccessKey Secret只显示这一次,后面将不再显示。
添加权限
2.3 示例代码
文档:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.625.18705ffa8u4lwj:
package com.oldlu.sso.service;
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;
/*
pom.xml
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.3</version>
</dependency>
*/
public class SendSms {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
"LTAI4G7d2Q9CHc741gighjTF", "uKOOGdIKvmoGhHlej8cJY8H3nlU6Fj");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", "cn-hangzhou");
request.putQueryParameter("PhoneNumbers", "158****7944"); //目标手机号
request.putQueryParameter("SignName", "ABC商城"); //签名名称
request.putQueryParameter("TemplateCode", "SMS_204756062"); //短信模板code
request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");//模板中变量替换
try {
CommonResponse response = client.getCommonResponse(request);
//{"Message":"OK","RequestId":"EC2D4C9A-0EAC-4213-BE45-CE6176E1DF23","BizId":"110903802851113360^0","Code":"OK"}
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
}
}
2.4 实现发送短信方法
配置文件:aliyun.properties
aliyun.sms.regionId = cn-hangzhou
aliyun.sms.accessKeyId = LTAI4G7d2Q9CHc741gighjTF
aliyun.sms.accessKeySecret = uKOOGdIKvmoGhHlej8cJY8H3nlU6Fj
aliyun.sms.domain= dysmsapi.aliyuncs.com
aliyun.sms.signName= ABC商城
aliyun.sms.templateCode= SMS_204756062
需要注意中文编码问题
读取配置:
package com.oldlu.sso.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:aliyun.properties")
@ConfigurationProperties(prefix = "aliyun.sms")
@Data
public class AliyunSMSConfig {
private String regionId;
private String accessKeyId;
private String accessKeySecret;
private String domain;
private String signName;
private String templateCode;
}
代码实现:
//SmsService.java
/**
* 通过阿里云发送验证码短信
*
* @param mobile
*/
public String sendSmsAliyun(String mobile) {
DefaultProfile profile = DefaultProfile.getProfile(
this.aliyunSMSConfig.getRegionId(),
this.aliyunSMSConfig.getAccessKeyId(),
this.aliyunSMSConfig.getAccessKeySecret());
IAcsClient client = new DefaultAcsClient(profile);
String code = RandomUtils.nextInt(100000, 999999) +"";
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain(this.aliyunSMSConfig.getDomain());
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("RegionId", this.aliyunSMSConfig.getRegionId());
request.putQueryParameter("PhoneNumbers", mobile);
request.putQueryParameter("SignName", this.aliyunSMSConfig.getSignName());
request.putQueryParameter("TemplateCode", this.aliyunSMSConfig.getTemplateCode());
request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
try {
CommonResponse response = client.getCommonResponse(request);
if(StringUtils.contains(response.getData(), "\"Code\":\"OK\"")){
return code;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}