ThirdPartFeignService
调用第三方服务发送信息
1. @FeignClient("gulimall-third-party") 2. public interface ThirdPartFeignService { 3. @GetMapping("/sms/sendcode") 4. public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code); 5. 6. }
sendCode
//60s内不能再发
//验证码校验redis 存key-phone value-code sms:code:13104928451->5379
//rdis缓存验证码,防止同一个phone在60s内再次发送验证码
1. @ResponseBody 2. @GetMapping("/sms/sendcode") 3. public R sendCode(@RequestParam("phone") String phone){ 4. String redisCode = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone); 5. if(!StringUtils.isEmpty(redisCode)){ 6. long l = Long.parseLong(redisCode.split("_")[1]); 7. if(System.currentTimeMillis()-l<60000){ 8. //60s内不能再发 9. return R.error(BizCodeEnume.SMS_CODE_EXCEPTION.getCode(), BizCodeEnume.SMS_CODE_EXCEPTION.getMsg()); 10. } 11. } 12. 13. //todo 接口防刷 14. 15. //验证码校验redis 存key-phone value-code sms:code:13104928451->5379 16. 17. String code = UUID.randomUUID().toString().substring(0, 5)+"_"+System.currentTimeMillis(); 18. //rdis缓存验证码,防止同一个phone在60s内再次发送验证码 19. redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,code,10, TimeUnit.MINUTES); 20. 21. thirdPartFeignService.sendCode(phone,code); 22. return R.ok(); 23. }
UserRegisteVo
接收前端传入的注册信息
1. @Data 2. public class UserRegisteVo { 3. @NotEmpty(message = "用户名必须提交") 4. @Length(min = 6,max = 18,message = "用户名必须是6-18位字符") 5. private String userName; 6. 7. @NotEmpty(message = "密码必须填写") 8. @Length(min = 6,max = 18,message = "密码必须是6-18位字符") 9. private String password; 10. 11. @NotEmpty(message = "手机号必须填写") 12. @Pattern(regexp = "^[1]([3-9]{9}$)",message = "手机号格式不正确") 13. private String phone; 14. 15. @NotEmpty(message = "验证码必须填写") 16. private String code; 17. 18. }
regist
* //todo 重定向携带数据,利用session原理,将数据放在session中 * 重要跳到下一个页面取出这个数据以后,session里面的数据就会删掉 * * //todo 分布式下的session问题 //重定向中保存数据 redirectAttributes.addFlashAttribute("errors",errors); //校验出错,转到注册页 //Request method 'POST' not supported //用户注册->/regist[post]--->转发/reg.html(路径映射默认都是get方式进行访问的.)
1. @PostMapping("/regist") 2. public String regist(@Valid UserRegisteVo vo, BindingResult result, RedirectAttributes redirectAttributes){ 3. if (result.hasErrors()){ 4. Map<String,String> errors=new HashMap<>(); 5. result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage)); 6. //重定向中保存数据 7. redirectAttributes.addFlashAttribute("errors",errors); 8. //校验出错,转到注册页 9. //Request method 'POST' not supported 10. //用户注册->/regist[post]--->转发/reg.html(路径映射默认都是get方式进行访问的.) 11. 12. return "redirect:http://auth.gulimall.com/reg.html"; 13. } 14. //真正的注册 15. //1.校验验证码 16. String code = vo.getCode(); 17. String s = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone()); 18. if(!StringUtils.isEmpty(s)){ 19. if(code.equals(s)){ 20. if(code.equals( s.split("_")[0])){ 21. //验证码通过 22. 23. 24. //删除验证码 25. redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX+vo.getPhone()); 26. 27. }else { 28. HashMap<String, String> errors = new HashMap<>(); 29. errors.put("code","验证码错误"); 30. redirectAttributes.addFlashAttribute("errord",errors); 31. return "redirect:http://auth.gulimall.com/reg.html"; 32. } 33. } 34. }else{ 35. HashMap<String, String> errors = new HashMap<>(); 36. errors.put("code","验证码错误"); 37. redirectAttributes.addFlashAttribute("errord",errors); 38. return "redirect:http://auth.gulimall.com/reg.html"; 39. } 40. 41. //注册成功返回到登录页 42. return "redirect:/login.html"; 43. }
前端
<div class="tips" style="color: red" th:text="${errors != null ? (#maps.containsKey(errors, 'userName') ? errors.userName : '') : ''}"> <div class="tips" style="color: red" th:text="${errors != null ? (#maps.containsKey(errors, 'password') ? errors.password : '') : ''}"> <div class="tips" style="color: red" th:text="${errors != null ? (#maps.containsKey(errors, 'phone') ? errors.phone : '') : ''}"> <div class="tips" style="color: red" th:text="${errors != null ? (#maps.containsKey(errors, 'code') ? errors.code : '') : ''}">