作者:LeoWang | 来源:互联网 | 2023-09-25 14:42
注册认证服务控制层面PostMapping(regist)publicStringregist(ValidUserRegisteVovo,BindingResultresul
注册
认证服务控制层面
@PostMapping("/regist")
public String regist(@Valid UserRegisteVo vo, BindingResult result, RedirectAttributes redirectAttributes){
if (result.hasErrors()){
Map errors&#61;new HashMap<>();
result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
//重定向中保存数据
redirectAttributes.addFlashAttribute("errors",errors);
//校验出错&#xff0c;转到注册页
//Request method &#39;POST&#39; not supported
//用户注册->/regist[post]--->转发/reg.html(路径映射默认都是get方式进行访问的.)
return "redirect:http://auth.gulimall.com/reg.html";
}
//真正的注册
//1.校验验证码
String code &#61; vo.getCode();
String s &#61; redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX &#43; vo.getPhone());
if(!StringUtils.isEmpty(s)){
if(code.equals(s)){
if(code.equals( s.split("_")[0])){
//删除验证码
redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX&#43;vo.getPhone());
//验证码通过
R r &#61; memberFeignService.register(vo);
if(r.getCode()&#61;&#61;0){
//成功
return "redirect:http://auth.gulimall.com/login.html";
}else {
HashMap errors &#61; new HashMap<>();
errors.put("msg",r.getData("msg",new TypeReference(){}));
redirectAttributes.addFlashAttribute("errors",errors);
return "redirect:http://auth.gulimall.com/reg.html";
}
}else {
HashMap errors &#61; new HashMap<>();
errors.put("code","验证码错误");
redirectAttributes.addFlashAttribute("errord",errors);
return "redirect:http://auth.gulimall.com/reg.html";
}
}
}else{
HashMap errors &#61; new HashMap<>();
errors.put("code","验证码错误");
redirectAttributes.addFlashAttribute("errord",errors);
return "redirect:http://auth.gulimall.com/reg.html";
}
//注册成功返回到登录页
return "redirect:https://auth.gulimall.com/login.html";
}
运程服务调用
&#64;PostMapping("member/member/registe")
R register(&#64;RequestBody UserRegisteVo vo);
&#64;Override
public void regist(MemberRegisterVo vo) {
MemberEntity entity &#61; new MemberEntity();
MemberDao memberDao &#61; this.baseMapper;
MemberLevelEntity levelEntity &#61;memberLevelDao.getDefaultLevel();
//设置默认登记
entity.setLevelId(levelEntity.getId());
//检查用户名和手机号是否唯一
checkPhoneUnique(vo.getPhone());
checkUsernameUnique(vo.getUserName());
entity.setMobile(vo.getPhone());
entity.setUsername(vo.getUserName());
//密码加密处理
BCryptPasswordEncoder passwordEncoder &#61; new BCryptPasswordEncoder();
String encode &#61; passwordEncoder.encode(vo.getPassword());
entity.setPassword(encode);
memberDao.insert(entity);
}
&#64;Override
public void checkPhoneUnique(String phone)throws PhoneExistException {
MemberDao memberDao &#61; this.baseMapper;
Integer count &#61; memberDao.selectCount(new QueryWrapper().eq("mobile", phone));
if(count>0){
throw new PhoneExistException();
}
}
&#64;Override
public void checkUsernameUnique(String username)throws UsernameExistException {
MemberDao memberDao &#61; this.baseMapper;
Integer count &#61; memberDao.selectCount(new QueryWrapper().eq("username", username));
if(count>0){
throw new UsernameExistException();
}
}
异常处理
public class UsernameExistException extends RuntimeException{
public UsernameExistException() {
super("用户名已存在");
}
}
public class PhoneExistException extends RuntimeException{
public PhoneExistException() {
super("电话已存在");
}
}
&#64;Override
public void checkPhoneUnique(String phone)throws PhoneExistException {
MemberDao memberDao &#61; this.baseMapper;
Integer count &#61; memberDao.selectCount(new QueryWrapper().eq("mobile", phone));
if(count>0){
throw new PhoneExistException();
}
}
&#64;Override
public void checkUsernameUnique(String username)throws UsernameExistException {
MemberDao memberDao &#61; this.baseMapper;
Integer count &#61; memberDao.selectCount(new QueryWrapper().eq("username", username));
if(count>0){
throw new UsernameExistException();
}
}
登录
认证服务控制层面
&#64;PostMapping("/login")
public String login(&#64;RequestBody UserLoginVo vo,RedirectAttributes redirectAttributes){
//运程登录
R login &#61; memberFeignService.login(vo);
if(login.getCode()&#61;&#61;0){
//成功
return "redirect:https://gulimall.com";
}else {
//失败
HashMap errors&#61;new HashMap<>();
errors.put("msg",login.getData("msg",new TypeReference(){}));
redirectAttributes.addFlashAttribute("errors",errors);
return "redirect:https://auth.gulimall.com/login.html";
}
}
运程调用服务
&#64;PostMapping("member/member/login")
R login(&#64;RequestBody UserLoginVo vo);
&#64;Override
public MemberEntity login(MemberLoginVo vo) {
String loginacct &#61; vo.getLoginacct();
String password &#61; vo.getPassword();
//1.去数据库查询
MemberDao baseMapper &#61; this.baseMapper;
MemberEntity entity &#61; baseMapper.selectOne(new QueryWrapper().eq("username", loginacct).
or().eq("mobile", loginacct));
if(entity&#61;&#61;null){
//登录失败
return null;
}else{
//1.获取数据库中密码
String passwordDb &#61; entity.getPassword();
BCryptPasswordEncoder encoder &#61; new BCryptPasswordEncoder();
//2.判断是否匹配
boolean matches &#61; encoder.matches(password, passwordDb);
if(matches){
return entity;
}else{
return null;
}
}
}
错误枚举类新增
USER_EXIST_EXCEPTION(15001,"用户存在异常"),
PHONE_EXIST_EXCEPTION(15002,"手机号存在异常"),
LOGINACCT_PASSWORD_INVAILD_EXCEPTION(15003,"账号或密码错误")
vo对象
UserLoginVo
&#64;Data
public class UserLoginVo {
private String loginacct;
private String password;
}
UserRegisteVo
&#64;Data
public class UserRegisteVo {
&#64;NotEmpty(message &#61; "用户名必须提交")
&#64;Length(min &#61; 6,max &#61; 18,message &#61; "用户名必须是6-18位字符")
private String userName;
&#64;NotEmpty(message &#61; "密码必须填写")
&#64;Length(min &#61; 6,max &#61; 18,message &#61; "密码必须是6-18位字符")
private String password;
&#64;NotEmpty(message &#61; "手机号必须填写")
&#64;Pattern(regexp &#61; "^[1]([3-9]{9}$)",message &#61; "手机号格式不正确")
private String phone;
&#64;NotEmpty(message &#61; "验证码必须填写")
private String code;
}