注册
认证服务控制层面
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. redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX+vo.getPhone()); 23. //验证码通过 24. R r = memberFeignService.register(vo); 25. if(r.getCode()==0){ 26. //成功 27. return "redirect:http://auth.gulimall.com/login.html"; 28. }else { 29. HashMap<String, String> errors = new HashMap<>(); 30. errors.put("msg",r.getData("msg",new TypeReference<String>(){})); 31. 32. redirectAttributes.addFlashAttribute("errors",errors); 33. return "redirect:http://auth.gulimall.com/reg.html"; 34. } 35. 36. }else { 37. HashMap<String, String> errors = new HashMap<>(); 38. errors.put("code","验证码错误"); 39. redirectAttributes.addFlashAttribute("errord",errors); 40. return "redirect:http://auth.gulimall.com/reg.html"; 41. } 42. } 43. }else{ 44. HashMap<String, String> errors = new HashMap<>(); 45. errors.put("code","验证码错误"); 46. redirectAttributes.addFlashAttribute("errord",errors); 47. return "redirect:http://auth.gulimall.com/reg.html"; 48. } 49. 50. //注册成功返回到登录页 51. return "redirect:https://auth.gulimall.com/login.html"; 52. }
运程服务调用
@PostMapping("member/member/registe")
R register(@RequestBody UserRegisteVo vo);
1. @Override 2. public void regist(MemberRegisterVo vo) { 3. MemberEntity entity = new MemberEntity(); 4. MemberDao memberDao = this.baseMapper; 5. MemberLevelEntity levelEntity =memberLevelDao.getDefaultLevel(); 6. //设置默认登记 7. entity.setLevelId(levelEntity.getId()); 8. //检查用户名和手机号是否唯一 9. checkPhoneUnique(vo.getPhone()); 10. checkUsernameUnique(vo.getUserName()); 11. entity.setMobile(vo.getPhone()); 12. entity.setUsername(vo.getUserName()); 13. //密码加密处理 14. BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); 15. String encode = passwordEncoder.encode(vo.getPassword()); 16. entity.setPassword(encode); 17. 18. 19. memberDao.insert(entity); 20. } 21. 22. @Override 23. public void checkPhoneUnique(String phone)throws PhoneExistException { 24. 25. MemberDao memberDao = this.baseMapper; 26. Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone)); 27. if(count>0){ 28. throw new PhoneExistException(); 29. } 30. 31. } 32. 33. @Override 34. public void checkUsernameUnique(String username)throws UsernameExistException { 35. MemberDao memberDao = this.baseMapper; 36. Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", username)); 37. if(count>0){ 38. throw new UsernameExistException(); 39. } 40. 41. }
异常处理
public class UsernameExistException extends RuntimeException{
public UsernameExistException() { super("用户名已存在"); } }
public class PhoneExistException extends RuntimeException{
public PhoneExistException() { super("电话已存在"); } }
1. @Override 2. public void checkPhoneUnique(String phone)throws PhoneExistException { 3. 4. MemberDao memberDao = this.baseMapper; 5. Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone)); 6. if(count>0){ 7. throw new PhoneExistException(); 8. } 9. 10. } 11. 12. @Override 13. public void checkUsernameUnique(String username)throws UsernameExistException { 14. MemberDao memberDao = this.baseMapper; 15. Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", username)); 16. if(count>0){ 17. throw new UsernameExistException(); 18. } 19. 20. }
登录
认证服务控制层面
1. @PostMapping("/login") 2. public String login(@RequestBody UserLoginVo vo,RedirectAttributes redirectAttributes){ 3. 4. //运程登录 5. R login = memberFeignService.login(vo); 6. if(login.getCode()==0){ 7. //成功 8. return "redirect:https://gulimall.com"; 9. }else { 10. //失败 11. HashMap<String,String> errors=new HashMap<>(); 12. errors.put("msg",login.getData("msg",new TypeReference<String>(){})); 13. redirectAttributes.addFlashAttribute("errors",errors); 14. 15. return "redirect:https://auth.gulimall.com/login.html"; 16. } 17. 18. 19. }
运程调用服务
@PostMapping("member/member/login")
R login(@RequestBody UserLoginVo vo);
1. @Override 2. public MemberEntity login(MemberLoginVo vo) { 3. String loginacct = vo.getLoginacct(); 4. 5. String password = vo.getPassword(); 6. 7. //1.去数据库查询 8. MemberDao baseMapper = this.baseMapper; 9. MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct). 10. or().eq("mobile", loginacct)); 11. 12. if(entity==null){ 13. //登录失败 14. return null; 15. }else{ 16. //1.获取数据库中密码 17. String passwordDb = entity.getPassword(); 18. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 19. //2.判断是否匹配 20. boolean matches = encoder.matches(password, passwordDb); 21. 22. if(matches){ 23. return entity; 24. }else{ 25. return null; 26. } 27. 28. } 29. 30. }
错误枚举类新增
USER_EXIST_EXCEPTION(15001,"用户存在异常"),
PHONE_EXIST_EXCEPTION(15002,"手机号存在异常"),
LOGINACCT_PASSWORD_INVAILD_EXCEPTION(15003,"账号或密码错误")
vo对象
UserLoginVo
@Data public class UserLoginVo { private String loginacct; private String password; }
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; }