diff --git a/api/src/main/java/com/simon/controller/OauthUserController.java b/api/src/main/java/com/simon/controller/OauthUserController.java index b5a86b87bf47a48ef213a4aaeff5b59b00bfec52..6d57c7c6dc7a37efb8049a73955c54b54097d3d4 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 2612919c2ba1ed4c0a2197d2c9aba1d75dac279a..f797f6255dab5b0db354e1d2f99df69ed9b6aefe 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 b27f9eb5b65fe605276a7e8ea4141b67334c2d6b..c317037bb105477687aa89748581ccf6318dc4ab 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 0000000000000000000000000000000000000000..d2e9050dc8725e2d0dcdb47f33672b4120cfdba5 --- /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 9aaea2f48dc8b3b31822e71e479ee20fc91cb3fb..ee1ccb0bb307a16000f7f5f3488a98b3a0c26a81 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 4e97c29ec0ec29b028a9cfebde920cec0400b197..6361e8f777f6b6cd160fd2e96f9262b795ccc437 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 9e66843ecba9e763c2ba9a19b406bcbd0e3818f9..55c0eb21755b81785d111e20e7b08571b9424c4d 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 b27f9eb5b65fe605276a7e8ea4141b67334c2d6b..c317037bb105477687aa89748581ccf6318dc4ab 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 0000000000000000000000000000000000000000..d2e9050dc8725e2d0dcdb47f33672b4120cfdba5 --- /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 ff0f38eace6258ce7a0a4cf03d518ad7c5bdf8f3..ee1ccb0bb307a16000f7f5f3488a98b3a0c26a81 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;