阿里云验证码
This commit is contained in:
parent
3bb8dfed1e
commit
4a25f8f5a3
5
pom.xml
5
pom.xml
|
|
@ -58,6 +58,11 @@
|
|||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>2.0.21</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,54 @@
|
|||
package com.chaozhanggui.system.cashierservice.controller;
|
||||
|
||||
import com.chaozhanggui.system.cashierservice.exception.MsgException;
|
||||
import com.chaozhanggui.system.cashierservice.sign.CodeEnum;
|
||||
import com.chaozhanggui.system.cashierservice.sign.Result;
|
||||
import com.chaozhanggui.system.cashierservice.util.RedisUtils;
|
||||
import com.chaozhanggui.system.cashierservice.util.StringUtil;
|
||||
import com.chaozhanggui.system.cashierservice.util.ValidateCodeUtil;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author lyf
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/phoneValidateCode")
|
||||
@RequiredArgsConstructor
|
||||
public class PhoneValidateCodeController {
|
||||
|
||||
private final ValidateCodeUtil validateCodeUtil;
|
||||
@Resource
|
||||
private RedisUtils redisUtils;
|
||||
/**
|
||||
* 一分钟
|
||||
*/
|
||||
protected static final long ONE_MINUTE = 60;
|
||||
|
||||
/**
|
||||
* 发送短信验证码
|
||||
* @param phone
|
||||
* @return
|
||||
*/
|
||||
@GetMapping
|
||||
public Result verifyPhoneIsExist(@RequestParam String phone) {
|
||||
|
||||
String random = StringUtil.random(6);
|
||||
validateCodeUtil.requestValidateCodeAli(phone, random);
|
||||
//存入缓存
|
||||
try {
|
||||
redisUtils.set(phone,random,ONE_MINUTE,TimeUnit.SECONDS);
|
||||
}catch (Exception e){
|
||||
throw new MsgException("验证码发送失败");
|
||||
}
|
||||
return Result.success(CodeEnum.SUCCESS);
|
||||
}
|
||||
}
|
||||
|
|
@ -38,7 +38,7 @@ public class OnlineUserService {
|
|||
onlineUserDto.setLoginTime(new Date());
|
||||
onlineUserDto.setShopId(shopId);
|
||||
try {
|
||||
redisUtils.set("online-token-"+token, onlineUserDto, MILLIS_MINUTE);
|
||||
// redisUtils.set("online-token-"+token, onlineUserDto, MILLIS_MINUTE);
|
||||
}catch (Exception e){
|
||||
throw new MsgException("登录错误");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -294,10 +294,10 @@ public class RedisUtils {
|
|||
* @param timeUnit 类型
|
||||
* @return true成功 false 失败
|
||||
*/
|
||||
public boolean set(String key, Object value, long time, TimeUnit timeUnit) {
|
||||
public boolean set(String key, String value, long time, TimeUnit timeUnit) {
|
||||
try {
|
||||
if (time > 0) {
|
||||
redisTemplate.opsForValue().set(key, value, time, timeUnit);
|
||||
stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
|
||||
} else {
|
||||
set(key, value);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,4 +51,14 @@ public class StringUtil {
|
|||
return JSON.parseArray(listString);
|
||||
|
||||
}
|
||||
|
||||
public static String random(int length) {
|
||||
Random random = new Random();
|
||||
String result = "";
|
||||
for (int i = 0; i < length; i++) {
|
||||
result += random.nextInt(10);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,85 @@
|
|||
package com.chaozhanggui.system.cashierservice.util;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.chaozhanggui.system.cashierservice.sign.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.chaozhanggui.system.cashierservice.sign.CodeEnum.SUCCESS;
|
||||
|
||||
/**
|
||||
* 获取验证码相关工具
|
||||
* @author 12847
|
||||
*/
|
||||
@Component
|
||||
public class ValidateCodeUtil {
|
||||
/**
|
||||
* 阿里云key
|
||||
*/
|
||||
@Value("${aliyun.keyid}")
|
||||
private String ACCESSKEYID;
|
||||
/**
|
||||
* 阿里云secret
|
||||
*/
|
||||
@Value("${aliyun.keysecret}")
|
||||
private String ACCESSKEYSECRET;
|
||||
/**
|
||||
* 十小时
|
||||
*/
|
||||
protected static final long MILLIS_MINUTE = 10 *60 * 60 *1000;
|
||||
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
|
||||
/**
|
||||
* 获取验证码(阿里云)
|
||||
*/
|
||||
public Result requestValidateCodeAli(String phone, String checkCode) {
|
||||
Client client = null;
|
||||
try {
|
||||
client = createClient();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// 1.发送短信
|
||||
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
|
||||
.setSignName("银收客")
|
||||
.setTemplateCode("SMS_244665149")
|
||||
.setTemplateParam("{\"code\":" +"'"+checkCode +"'"+"}")
|
||||
.setPhoneNumbers(phone);
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
assert client != null;
|
||||
return Result.success(SUCCESS,client.sendSmsWithOptions(sendSmsRequest, runtime));
|
||||
} catch (Exception e) {
|
||||
return Result.fail(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 发送短信(阿里云)
|
||||
*
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public Client createClient() throws Exception {
|
||||
Config config = new Config();
|
||||
config.accessKeyId = ACCESSKEYID;
|
||||
config.accessKeySecret = ACCESSKEYSECRET;
|
||||
return new com.aliyun.dysmsapi20170525.Client(config);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue