From 42f46d6fb91d7fb566e9331445e7e04caa51bb9d Mon Sep 17 00:00:00 2001 From: SimonSun Date: Wed, 2 Jan 2019 14:03:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=9F=AD=E4=BF=A1=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E6=9C=8D=E5=8A=A1=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simon/controller/OauthUserController.java | 2 + .../simon/controller/VeriCodeController.java | 110 ++--------------- .../java/com/simon/service/SmsService.java | 4 +- .../simon/service/impl/AliSmsServiceImpl.java | 101 ++++++++++++++++ ...erviceImpl.java => YzxSmsServiceImpl.java} | 8 +- .../simon/controller/OauthUserController.java | 2 + .../simon/controller/VeriCodeController.java | 114 ++---------------- .../java/com/simon/service/SmsService.java | 4 +- .../simon/service/impl/AliSmsServiceImpl.java | 101 ++++++++++++++++ ...erviceImpl.java => YzxSmsServiceImpl.java} | 12 +- 10 files changed, 246 insertions(+), 212 deletions(-) create mode 100644 api/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java rename api/src/main/java/com/simon/service/impl/{SmsServiceImpl.java => YzxSmsServiceImpl.java} (94%) create mode 100644 web/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java rename web/src/main/java/com/simon/service/impl/{SmsServiceImpl.java => YzxSmsServiceImpl.java} (91%) diff --git a/api/src/main/java/com/simon/controller/OauthUserController.java b/api/src/main/java/com/simon/controller/OauthUserController.java index b5a86b8..6d57c7c 100644 --- a/api/src/main/java/com/simon/controller/OauthUserController.java +++ b/api/src/main/java/com/simon/controller/OauthUserController.java @@ -16,6 +16,7 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.MessageSource; import org.springframework.security.core.Authentication; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -42,6 +43,7 @@ public class OauthUserController extends BaseController { private OauthUserService oauthUserService; @Autowired + @Qualifier(value = "yzxSmsServiceImpl") private SmsService smsService; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(11); diff --git a/api/src/main/java/com/simon/controller/VeriCodeController.java b/api/src/main/java/com/simon/controller/VeriCodeController.java index 2612919..f797f62 100644 --- a/api/src/main/java/com/simon/controller/VeriCodeController.java +++ b/api/src/main/java/com/simon/controller/VeriCodeController.java @@ -3,123 +3,35 @@ package com.simon.controller; import com.simon.common.controller.BaseController; import com.simon.common.domain.ResultCode; import com.simon.common.domain.ResultMsg; -import com.simon.model.VeriCode; -import com.simon.repository.VeriCodeRepository; import com.simon.service.SmsService; import com.taobao.api.ApiException; -import com.taobao.api.DefaultTaobaoClient; -import com.taobao.api.domain.BizResult; -import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; -import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import lombok.var; -import org.apache.commons.lang3.RandomUtils; -import org.apache.log4j.Logger; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; /** - * Created by simon on 2016/9/19. + * @author simon + * @date 2019-01-02 */ +@Slf4j @Api(value = "验证码", description = "验证码") @RestController @RequestMapping("/api/veriCodes") public class VeriCodeController extends BaseController { - private static Logger logger = Logger.getLogger(VeriCodeController.class); @Autowired + @Qualifier(value = "yzxSmsServiceImpl") private SmsService smsService; - @Autowired - private VeriCodeRepository veriCodeRepository; - - @Value("${com.alibaba.dayu.url.sandbox}") - private String DAYU_URL_SANDBOX; - - @Value("${com.alibaba.dayu.url.real}") - private String DAYU_URL_REAL; - - @Value("${com.alibaba.dayu.app-key}") - private String DAYU_APP_KEY; - - @Value("${com.alibaba.dayu.app-secret}") - private String DAYU_APP_SECRET; - - @Value("${com.alibaba.dayu.sms-type}") - private String DAYU_SMS_TYPE; - - @Value("${com.alibaba.dayu.sms-free-sign-name}") - private String DAYU_SMS_FREE_SIGN_NAME; - - @Value("${com.alibaba.dayu.sms-template-code}") - private String DAYU_SMS_TEMPLATE_CODE; - - @ApiOperation(value = "获取验证码", notes = "验证码有效时间是30分钟,验证码在失效前5分钟就会重新生成一个返回,给用户通过验证码修改密码足够的时间") - /*@ApiImplicitParam(name = "phone", value = "用户手机号", required = true, dataType = "string")*/ - @RequestMapping(value = "/getRegisterCode", method = RequestMethod.GET) - public ResultMsg getVeriCode(@RequestParam String phone){ - - var veriCode = veriCodeRepository.findByPhone(phone); - if (null==veriCode){ - veriCode = new VeriCode(); - veriCode.setPhone(phone); - veriCode.setCode(RandomUtils.nextInt(100000, 999999)); - veriCode.setCreateTime(System.currentTimeMillis()); - veriCode.setExpires(30*60); - veriCodeRepository.save(veriCode); - }else{ - if (System.currentTimeMillis()>(veriCode.getCreateTime()+veriCode.getExpires()-60*5)){ - veriCode.setCode(RandomUtils.nextInt(100000, 999999)); - veriCode.setCreateTime(System.currentTimeMillis()); - veriCode.setExpires(30*60); - veriCodeRepository.save(veriCode); - } - } - var client = new DefaultTaobaoClient( - DAYU_URL_REAL, DAYU_APP_KEY, DAYU_APP_SECRET); - var req = new AlibabaAliqinFcSmsNumSendRequest(); - req.setExtend(""); - req.setSmsType(DAYU_SMS_TYPE); - req.setSmsFreeSignName(DAYU_SMS_FREE_SIGN_NAME); - req.setSmsParamString("{veriCode:'"+veriCode.getCode()+"'}"); - req.setRecNum(phone); - req.setSmsTemplateCode(DAYU_SMS_TEMPLATE_CODE); - AlibabaAliqinFcSmsNumSendResponse rsp = null; - try { - rsp = client.execute(req); - BizResult bizResult = rsp.getResult(); - if (null != bizResult && bizResult.getSuccess()){ - return new ResultMsg(200, "验证码已发送"); - }else{ - logger.error("请确认阿里大于账号还有余额"); - return ResultMsg.fail(500, "验证码发送失败,请稍后重试"); - } - } catch (ApiException e) { - e.printStackTrace(); - return ResultMsg.fail(500, e.getErrMsg()); - } - } - - @ApiOperation(value = "校验验证码") - @RequestMapping(value = "/checkVeriCode", method = RequestMethod.GET) - public ResultMsg checkVeriCode(@RequestParam String phone, @RequestParam Integer code){ - try{ - var veriCode = veriCodeRepository.findByPhoneAndCode(phone, code); - if (null!=veriCode){ - return ResultMsg.success(200, "验证码正确"); - }else{ - return ResultMsg.fail(404, "验证码错误"); - } - }catch (Exception e){ - return ResultMsg.fail(500, "验证码错误", e.getMessage()); - } - } - @ApiOperation(value = "获取验证码") @GetMapping("/sms/{phone}") - public ResultMsg getVeriCodeByPhone(@PathVariable String phone){ + public ResultMsg getVeriCodeByPhone(@PathVariable String phone) throws ApiException { if (smsService.sendIdentifyCode(phone)){ return ResultMsg.success(); }else{ diff --git a/api/src/main/java/com/simon/service/SmsService.java b/api/src/main/java/com/simon/service/SmsService.java index b27f9eb..c317037 100644 --- a/api/src/main/java/com/simon/service/SmsService.java +++ b/api/src/main/java/com/simon/service/SmsService.java @@ -1,5 +1,7 @@ package com.simon.service; +import com.taobao.api.ApiException; + /** * 短信服务 * @@ -8,7 +10,7 @@ package com.simon.service; **/ public interface SmsService { - boolean sendIdentifyCode(String mobile); + boolean sendIdentifyCode(String mobile) throws ApiException; boolean checkCode(String mobile,String code); } diff --git a/api/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java b/api/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java new file mode 100644 index 0000000..d2e9050 --- /dev/null +++ b/api/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java @@ -0,0 +1,101 @@ +package com.simon.service.impl; + +import com.simon.service.SmsService; +import com.taobao.api.ApiException; +import com.taobao.api.DefaultTaobaoClient; +import com.taobao.api.domain.BizResult; +import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; +import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; +import lombok.extern.slf4j.Slf4j; +import lombok.var; +import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * 阿里大于短信验证码服务 + * + * @author simon + * @date 2019-01-02 + **/ +@Slf4j +@Service(value = "aliSmsServiceImpl") +@Transactional(rollbackFor = {Exception.class}) +public class AliSmsServiceImpl implements SmsService { + @Value("${com.alibaba.dayu.url.sandbox}") + private String DAYU_URL_SANDBOX; + + @Value("${com.alibaba.dayu.url.real}") + private String DAYU_URL_REAL; + + @Value("${com.alibaba.dayu.app-key}") + private String DAYU_APP_KEY; + + @Value("${com.alibaba.dayu.app-secret}") + private String DAYU_APP_SECRET; + + @Value("${com.alibaba.dayu.sms-type}") + private String DAYU_SMS_TYPE; + + @Value("${com.alibaba.dayu.sms-free-sign-name}") + private String DAYU_SMS_FREE_SIGN_NAME; + + @Value("${com.alibaba.dayu.sms-template-code}") + private String DAYU_SMS_TEMPLATE_CODE; + + @Autowired + private org.springframework.cache.CacheManager cacheManager; + + @Override + public boolean sendIdentifyCode(String mobile) throws ApiException { + var code = RandomUtils.nextInt(100000, 999999); + var client = new DefaultTaobaoClient( + DAYU_URL_REAL, DAYU_APP_KEY, DAYU_APP_SECRET); + var req = new AlibabaAliqinFcSmsNumSendRequest(); + req.setExtend(""); + req.setSmsType(DAYU_SMS_TYPE); + req.setSmsFreeSignName(DAYU_SMS_FREE_SIGN_NAME); + req.setSmsParamString("{veriCode:'"+code+"'}"); + req.setRecNum(mobile); + req.setSmsTemplateCode(DAYU_SMS_TEMPLATE_CODE); + AlibabaAliqinFcSmsNumSendResponse rsp = null; + rsp = client.execute(req); + BizResult bizResult = rsp.getResult(); + if (null != bizResult && bizResult.getSuccess()){ + //写入缓存 + var cache = cacheManager.getCache("smsCache"); + cache.put(mobile, code); + + return true; + }else{ + log.error("请确认阿里大于账号还有余额"); + return false; + } + } + + @Override + public boolean checkCode(String mobile, String code) { + log.info("checkCode"); + var cache = cacheManager.getCache("smsCache"); + var ele = cache.get(mobile); + String output = (ele == null ? null : ele.get().toString()); + log.info("从缓存中读到" + mobile + "," + output); + + var result = false; + + if (StringUtils.isNotEmpty(code) && StringUtils.isNotEmpty(output)) { + if (code.equals(output)) { + result = true; + //cache.evict(mobile);//删除 + } + } + + //删除缓存 + cache.evict(mobile); + + return result; + } +} diff --git a/api/src/main/java/com/simon/service/impl/SmsServiceImpl.java b/api/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java similarity index 94% rename from api/src/main/java/com/simon/service/impl/SmsServiceImpl.java rename to api/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java index 9aaea2f..ee1ccb0 100644 --- a/api/src/main/java/com/simon/service/impl/SmsServiceImpl.java +++ b/api/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java @@ -15,16 +15,16 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** - * 短信服务 + * 云之讯短信服务 * * @author simon * @date 2018-12-03 **/ @Slf4j -@Service +@Service(value = "yzxSmsServiceImpl") @Transactional(rollbackFor = {Exception.class}) -public class SmsServiceImpl implements SmsService { +public class YzxSmsServiceImpl implements SmsService { private static final long EXPIRE_SECONDS = 300 * 1000; @@ -58,7 +58,7 @@ public class SmsServiceImpl implements SmsService { if ("0".equals(responseCode)) { log.info("发送成功"); ret = true; - //写入redis缓存 + //写入缓存 var cache = cacheManager.getCache("smsCache"); cache.put(mobile, code); } diff --git a/web/src/main/java/com/simon/controller/OauthUserController.java b/web/src/main/java/com/simon/controller/OauthUserController.java index 4e97c29..6361e8f 100644 --- a/web/src/main/java/com/simon/controller/OauthUserController.java +++ b/web/src/main/java/com/simon/controller/OauthUserController.java @@ -16,6 +16,7 @@ import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.MessageSource; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; @@ -47,6 +48,7 @@ public class OauthUserController extends BaseController { private OauthUserService oauthUserService; @Autowired + @Qualifier(value = "yzxSmsServiceImpl") private SmsService smsService; PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(11); diff --git a/web/src/main/java/com/simon/controller/VeriCodeController.java b/web/src/main/java/com/simon/controller/VeriCodeController.java index 9e66843..55c0eb2 100644 --- a/web/src/main/java/com/simon/controller/VeriCodeController.java +++ b/web/src/main/java/com/simon/controller/VeriCodeController.java @@ -3,125 +3,37 @@ package com.simon.controller; import com.simon.common.controller.BaseController; import com.simon.common.domain.ResultCode; import com.simon.common.domain.ResultMsg; -import com.simon.model.VeriCode; -import com.simon.repository.VeriCodeRepository; import com.simon.service.SmsService; import com.taobao.api.ApiException; -import com.taobao.api.DefaultTaobaoClient; -import com.taobao.api.domain.BizResult; -import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; -import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import lombok.var; -import org.apache.commons.lang3.RandomUtils; -import org.apache.log4j.Logger; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.web.bind.annotation.*; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; /** - * Created by simon on 2016/9/19. + * @author simon + * @date 2019-01-02 */ +@Slf4j @Api(value = "验证码", description = "验证码") @RestController @RequestMapping("/api/veriCodes") public class VeriCodeController extends BaseController { - private static Logger logger = Logger.getLogger(VeriCodeController.class); - @Autowired + @Qualifier(value = "yzxSmsServiceImpl") private SmsService smsService; - @Autowired - private VeriCodeRepository veriCodeRepository; - - @Value("${com.alibaba.dayu.url.sandbox}") - private String DAYU_URL_SANDBOX; - - @Value("${com.alibaba.dayu.url.real}") - private String DAYU_URL_REAL; - - @Value("${com.alibaba.dayu.app-key}") - private String DAYU_APP_KEY; - - @Value("${com.alibaba.dayu.app-secret}") - private String DAYU_APP_SECRET; - - @Value("${com.alibaba.dayu.sms-type}") - private String DAYU_SMS_TYPE; - - @Value("${com.alibaba.dayu.sms-free-sign-name}") - private String DAYU_SMS_FREE_SIGN_NAME; - - @Value("${com.alibaba.dayu.sms-template-code}") - private String DAYU_SMS_TEMPLATE_CODE; - - @ApiOperation(value = "获取验证码", notes = "验证码有效时间是30分钟,验证码在失效前5分钟就会重新生成一个返回,给用户通过验证码修改密码足够的时间") - @RequestMapping(value = "/getRegisterCode", method = RequestMethod.GET) - public ResultMsg getVeriCode(@RequestParam String phone) { - - var veriCode = veriCodeRepository.findByPhone(phone); - if (null == veriCode) { - veriCode = new VeriCode(); - veriCode.setPhone(phone); - veriCode.setCode(RandomUtils.nextInt(100000, 999999)); - veriCode.setCreateTime(System.currentTimeMillis()); - veriCode.setExpires(30 * 60); - veriCodeRepository.save(veriCode); - } else { - if (System.currentTimeMillis() > (veriCode.getCreateTime() + veriCode.getExpires() - 60 * 5)) { - veriCode.setCode(RandomUtils.nextInt(100000, 999999)); - veriCode.setCreateTime(System.currentTimeMillis()); - veriCode.setExpires(30 * 60); - veriCodeRepository.save(veriCode); - } - } - var client = new DefaultTaobaoClient( - DAYU_URL_REAL, DAYU_APP_KEY, DAYU_APP_SECRET); - var req = new AlibabaAliqinFcSmsNumSendRequest(); - req.setExtend(""); - req.setSmsType(DAYU_SMS_TYPE); - req.setSmsFreeSignName(DAYU_SMS_FREE_SIGN_NAME); - req.setSmsParamString("{veriCode:'" + veriCode.getCode() + "'}"); - req.setRecNum(phone); - req.setSmsTemplateCode(DAYU_SMS_TEMPLATE_CODE); - AlibabaAliqinFcSmsNumSendResponse rsp = null; - try { - rsp = client.execute(req); - BizResult bizResult = rsp.getResult(); - if (null != bizResult && bizResult.getSuccess()) { - return new ResultMsg(200, "验证码已发送"); - } else { - logger.error("请确认阿里大于账号还有余额"); - return ResultMsg.fail(500, "验证码发送失败,请稍后重试"); - } - } catch (ApiException e) { - e.printStackTrace(); - return ResultMsg.fail(500, e.getErrMsg()); - } - } - - @ApiOperation(value = "校验验证码") - @RequestMapping(value = "/checkVeriCode", method = RequestMethod.GET) - public ResultMsg checkVeriCode(@RequestParam String phone, @RequestParam Integer code) { - try { - var veriCode = veriCodeRepository.findByPhoneAndCode(phone, code); - if (null != veriCode) { - return ResultMsg.success(200, "验证码正确"); - } else { - return ResultMsg.fail(404, "验证码错误"); - } - } catch (Exception e) { - return ResultMsg.fail(500, "验证码错误", e.getMessage()); - } - } - @ApiOperation(value = "获取验证码") @GetMapping("/sms/{phone}") - public ResultMsg getVeriCodeByPhone(@PathVariable String phone) { - if (smsService.sendIdentifyCode(phone)) { + public ResultMsg getVeriCodeByPhone(@PathVariable String phone) throws ApiException { + if (smsService.sendIdentifyCode(phone)){ return ResultMsg.success(); - } else { + }else{ return ResultMsg.fail(ResultCode.FAIL); } } diff --git a/web/src/main/java/com/simon/service/SmsService.java b/web/src/main/java/com/simon/service/SmsService.java index b27f9eb..c317037 100644 --- a/web/src/main/java/com/simon/service/SmsService.java +++ b/web/src/main/java/com/simon/service/SmsService.java @@ -1,5 +1,7 @@ package com.simon.service; +import com.taobao.api.ApiException; + /** * 短信服务 * @@ -8,7 +10,7 @@ package com.simon.service; **/ public interface SmsService { - boolean sendIdentifyCode(String mobile); + boolean sendIdentifyCode(String mobile) throws ApiException; boolean checkCode(String mobile,String code); } diff --git a/web/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java b/web/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java new file mode 100644 index 0000000..d2e9050 --- /dev/null +++ b/web/src/main/java/com/simon/service/impl/AliSmsServiceImpl.java @@ -0,0 +1,101 @@ +package com.simon.service.impl; + +import com.simon.service.SmsService; +import com.taobao.api.ApiException; +import com.taobao.api.DefaultTaobaoClient; +import com.taobao.api.domain.BizResult; +import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest; +import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse; +import lombok.extern.slf4j.Slf4j; +import lombok.var; +import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * 阿里大于短信验证码服务 + * + * @author simon + * @date 2019-01-02 + **/ +@Slf4j +@Service(value = "aliSmsServiceImpl") +@Transactional(rollbackFor = {Exception.class}) +public class AliSmsServiceImpl implements SmsService { + @Value("${com.alibaba.dayu.url.sandbox}") + private String DAYU_URL_SANDBOX; + + @Value("${com.alibaba.dayu.url.real}") + private String DAYU_URL_REAL; + + @Value("${com.alibaba.dayu.app-key}") + private String DAYU_APP_KEY; + + @Value("${com.alibaba.dayu.app-secret}") + private String DAYU_APP_SECRET; + + @Value("${com.alibaba.dayu.sms-type}") + private String DAYU_SMS_TYPE; + + @Value("${com.alibaba.dayu.sms-free-sign-name}") + private String DAYU_SMS_FREE_SIGN_NAME; + + @Value("${com.alibaba.dayu.sms-template-code}") + private String DAYU_SMS_TEMPLATE_CODE; + + @Autowired + private org.springframework.cache.CacheManager cacheManager; + + @Override + public boolean sendIdentifyCode(String mobile) throws ApiException { + var code = RandomUtils.nextInt(100000, 999999); + var client = new DefaultTaobaoClient( + DAYU_URL_REAL, DAYU_APP_KEY, DAYU_APP_SECRET); + var req = new AlibabaAliqinFcSmsNumSendRequest(); + req.setExtend(""); + req.setSmsType(DAYU_SMS_TYPE); + req.setSmsFreeSignName(DAYU_SMS_FREE_SIGN_NAME); + req.setSmsParamString("{veriCode:'"+code+"'}"); + req.setRecNum(mobile); + req.setSmsTemplateCode(DAYU_SMS_TEMPLATE_CODE); + AlibabaAliqinFcSmsNumSendResponse rsp = null; + rsp = client.execute(req); + BizResult bizResult = rsp.getResult(); + if (null != bizResult && bizResult.getSuccess()){ + //写入缓存 + var cache = cacheManager.getCache("smsCache"); + cache.put(mobile, code); + + return true; + }else{ + log.error("请确认阿里大于账号还有余额"); + return false; + } + } + + @Override + public boolean checkCode(String mobile, String code) { + log.info("checkCode"); + var cache = cacheManager.getCache("smsCache"); + var ele = cache.get(mobile); + String output = (ele == null ? null : ele.get().toString()); + log.info("从缓存中读到" + mobile + "," + output); + + var result = false; + + if (StringUtils.isNotEmpty(code) && StringUtils.isNotEmpty(output)) { + if (code.equals(output)) { + result = true; + //cache.evict(mobile);//删除 + } + } + + //删除缓存 + cache.evict(mobile); + + return result; + } +} diff --git a/web/src/main/java/com/simon/service/impl/SmsServiceImpl.java b/web/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java similarity index 91% rename from web/src/main/java/com/simon/service/impl/SmsServiceImpl.java rename to web/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java index ff0f38e..ee1ccb0 100644 --- a/web/src/main/java/com/simon/service/impl/SmsServiceImpl.java +++ b/web/src/main/java/com/simon/service/impl/YzxSmsServiceImpl.java @@ -15,16 +15,16 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** - * 短信服务 + * 云之讯短信服务 * * @author simon * @date 2018-12-03 **/ @Slf4j -@Service +@Service(value = "yzxSmsServiceImpl") @Transactional(rollbackFor = {Exception.class}) -public class SmsServiceImpl implements SmsService { +public class YzxSmsServiceImpl implements SmsService { private static final long EXPIRE_SECONDS = 300 * 1000; @@ -58,10 +58,9 @@ public class SmsServiceImpl implements SmsService { if ("0".equals(responseCode)) { log.info("发送成功"); ret = true; - //写入redis缓存 + //写入缓存 var cache = cacheManager.getCache("smsCache"); cache.put(mobile, code); - } } } @@ -72,10 +71,11 @@ public class SmsServiceImpl implements SmsService { @Override public boolean checkCode(String mobile, String code) { + log.info("checkCode"); var cache = cacheManager.getCache("smsCache"); var ele = cache.get(mobile); String output = (ele == null ? null : ele.get().toString()); - log.info(mobile + "," + output); + log.info("从缓存中读到" + mobile + "," + output); var result = false; -- Gitee