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