注意:有哪里看不懂的或者是想看完整的注册、验证、登录、第三方登录等流程和代码,请查看本专栏文章。
1、编写认证服务调用会员服务的service.
package com.atguigu.***.auth.feign;
import com.atguigu.common.utils.R;
import com.atguigu.***.auth.vo.UserLoginVo;
import com.atguigu.***.auth.vo.UserRegistVo;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
/**
* @author guanghaocheng
* @version 1.0
* 翼以尘雾之微补益山海,荧烛末光增辉日月
* @date 2021/7/8 19:53
*/
@FeignClient("***-member")
public interface MemberFeignService {
@PostMapping("/member/member/regist")
public R regist(@RequestBody UserRegistVo vo);
@PostMapping("member/member/login")
public R login(@RequestBody UserLoginVo vo);
}
2、认证服务登录的接口。
@PostMapping("/login")
public String login(UserLoginVo vo,RedirectAttributes redirectAttributes){
//远程登录
R r = memberFeignService.login(vo);
if(r.getCode() == 0){
//登录成功
return "redirect:http://***.com";
}else{
Map<String,String> errors = new HashMap<>();
errors.put("msg",r.getData("msg",new TypeReference<String>(){}));
redirectAttributes.addFlashAttribute("errors",errors);
return "redirect:http://auth.***.com/login.html";
}
}
3、会员模块的登录接口。
@PostMapping("/login")
public R login(@RequestBody MemberLoginVo vo){
MemberEntity entity = memberService.login(vo);
if(entity != null){
//TODO 1、登录成功的获取数据及处理
return R.ok();
}else{
return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
}
}
4、会员模块的登录service实现。
@Override
public MemberEntity login(MemberLoginVo vo) {
String loginacct = vo.getLoginacct();
String password = vo.getPassword();
//1、去数据库查询 select * from ums_member where username = ? or mobile = ? or email = ?
MemberDao baseMapper = this.baseMapper;
MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct).or().eq("mobile", loginacct).or().eq("email", loginacct));
if(entity == null){
//根据用户名没查到信息,则直接登录失败
return null;
}else{
//根据用户名查到信息了,进行密码比对
String passwordDB = entity.getPassword();//获取数据库存的密码
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
boolean matches = bCryptPasswordEncoder.matches(password, passwordDB);
if(matches){
//登录成功
return entity;
}else{
//登录失败
return null;
}
}
// return null;
}