阿里云短信平台集成

This commit is contained in:
udf_admin@163.com
2018-01-31 22:58:15 +08:00
parent fea0284fc8
commit 681580cf93
3 changed files with 148 additions and 4 deletions
@@ -0,0 +1,123 @@
package org.itkk.udf.aliyun.dysms.web;
import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.itkk.udf.aliyun.dysms.api.DysmsWarpper;
import org.itkk.udf.core.RestResponse;
import org.itkk.udf.core.exception.ParameterValidException;
import org.itkk.udf.core.utils.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* AliuyunDysmsController
*/
@RestController
public class AliuyunDysmsController implements IAliuyunDysmsController {
/**
* VERIFICATION_CODE_CACHE_KEY_PREFIX
*/
private static final String VERIFICATION_CODE_CACHE_KEY_PREFIX = ":VERIFICATION_CODE:";
/**
* redisTemplate
*/
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
/**
* profiles
*/
@Value("${spring.profiles.active}")
private String profiles;
/**
* dysmsWarpper
*/
@Autowired
private DysmsWarpper dysmsWarpper;
/**
* httpServletRequest
*/
@Autowired
private HttpServletRequest httpServletRequest;
/**
* objectMapper
*/
@Autowired
private ObjectMapper objectMapper;
@Override
public RestResponse<Boolean> sendVerificationCode() throws JsonProcessingException, ClientException {
//常量
final String signName = "漓淼汽车科技";
final String templateCode = "SMS_123798312";
final int num4 = 4;
final int num5 = 5;
//获得手机号
String phoneNumber = httpServletRequest.getHeader("phoneNumber");
//判空
if (StringUtils.isBlank(phoneNumber)) {
throw new ParameterValidException("手机号不能为空", null);
}
//获得验证码
String code = IdUtil.genRandomNumber(num4);
//放入缓存
String key = getKey(phoneNumber);
redisTemplate.opsForValue().set(key, code, num5, TimeUnit.MINUTES);
//构造参数
Map<String, String> param = new HashMap<>();
param.put("code", code);
//发送 & 返回
return new RestResponse<>(dysmsWarpper.send(phoneNumber, signName, templateCode, objectMapper.writeValueAsString(param)));
}
@Override
public RestResponse<Boolean> checkVerificationCode() {
//获得手机号
String phoneNumber = httpServletRequest.getHeader("phoneNumber");
//判空
if (StringUtils.isBlank(phoneNumber)) {
throw new ParameterValidException("手机号不能为空", null);
}
//获得验证码
String verificationCode = httpServletRequest.getHeader("verificationCode");
//判空
if (StringUtils.isBlank(verificationCode)) {
throw new ParameterValidException("验证码不能为空", null);
}
//获得缓存值
String key = getKey(phoneNumber);
Object cacheValueObject = redisTemplate.opsForValue().get(key);
//比对验证
boolean result = cacheValueObject != null && cacheValueObject.equals(verificationCode);
//如果校验通过,删除缓存值
if (result) {
redisTemplate.delete(key);
}
//返回
return new RestResponse<>(result);
}
/**
* getKey
*
* @param phoneNumber phoneNumber
* @return String
*/
private String getKey(String phoneNumber) {
return profiles + VERIFICATION_CODE_CACHE_KEY_PREFIX + phoneNumber;
}
}
@@ -6,6 +6,8 @@
*/
package org.itkk.udf.aliyun.dysms.web;
import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
@@ -25,16 +27,18 @@ import org.springframework.web.bind.annotation.RequestMethod;
public interface IAliuyunDysmsController {
/**
* 描述 : 发送短信验证码
* 发送短信验证码
*
* @return RestResponse<Boolean>
* @return Boolean
* @throws JsonProcessingException JsonProcessingException
* @throws ClientException ClientException
*/
@ApiOperation(value = "ALIYUN_DYSMS_SEND_VERIFICATION_CODE", notes = "发送短信验证码")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "phoneNumber", value = "手机号码", required = true, dataType = "string")
})
@RequestMapping(value = "send/verification/code", method = RequestMethod.POST)
RestResponse<Boolean> sendVerificationCode();
RestResponse<Boolean> sendVerificationCode() throws JsonProcessingException, ClientException;
/**
* 描述 : 验证短信验证码
@@ -43,6 +47,7 @@ public interface IAliuyunDysmsController {
*/
@ApiOperation(value = "ALIYUN_DYSMS_CHECK_VERIFICATION_CODE", notes = "验证短信验证码")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "header", name = "phoneNumber", value = "手机号码", required = true, dataType = "string"),
@ApiImplicitParam(paramType = "header", name = "verificationCode", value = "短信验证码", required = true, dataType = "string")
})
@RequestMapping(value = "check/verification/code", method = RequestMethod.POST)
@@ -34,7 +34,23 @@ public class IdUtil {
}
/**
* 生成6位随机字符串(0-9,A-Z)
* 生成随机字符串(0-9)
*
* @param length length
* @return String
*/
public static String genRandomNumber(int length) {
final String source = "1234567890";
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(source.charAt(random.nextInt(source.length() - 1)));
}
return sb.toString();
}
/**
* 生成随机字符串(0-9,A-Z)
*
* @param length length
* @return String