文章目录
- 1、单点登录解决方案
-
- 2、user服务-登录接口
- 2.1、UserController
- 2.2、UserInfoServiceImpl
- 2.3、载荷
- 2.4、响应
- 2.5、Redis Desktop Manager
1、单点登录解决方案
1.1、后端保存登录状态
1.2、token模式
2、user服务-登录接口
2.1、UserController
@Operation( summary = "用户登录接口")
@PostMapping("/login")
public Result login(@RequestBody UserInfo userInfo) {
String token = userInfoService.login(userInfo);
return Result.ok(token);
}
2.2、UserInfoServiceImpl
@Override
public String login(UserInfo userInfo) {
String username = userInfo.getUsername();
String password = userInfo.getPassword();
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
throw new SpzxException(ResultCodeEnum.LOGIN_PARAMS_ERROR, null);
}
UserInfo dbUserInfo = this.getOne(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUsername, username));
if (dbUserInfo == null) {
throw new SpzxException(ResultCodeEnum.LOGIN_USERNAME_ERROR, null);
}
if (dbUserInfo.getStatus() != 1) {
throw new SpzxException(ResultCodeEnum.LOGIN_USER_STATUS_ERROR, null);
}
String encodePwd = DigestUtils.md5DigestAsHex((
DigestUtils.md5DigestAsHex(password.getBytes()) + dbUserInfo.getSalt()).getBytes());
if (!encodePwd.equals(dbUserInfo.getPassword())) {
throw new SpzxException(ResultCodeEnum.LOGIN_PASSWORD_ERROR, null);
}
String token = IdUtil.getSnowflake(1, 1).nextIdStr();
dbUserInfo.setPassword(null);
dbUserInfo.setSalt(null);
redisTemplate.opsForValue().set("spzx:user:login:" + token, dbUserInfo, 7, TimeUnit.DAYS);
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
return token;
}
2.3、载荷
{
"username": "18947628476",
"password": "123456"
}
2.4、响应
{"code":200,"message":"SUCCESS","data":"1801188891328385024"}
2.5、Redis Desktop Manager
{
"@class": "com.atguigu.spzx.model.entity.h5.UserInfo",
"id": 1774759294596579329,
"createTime": [
"java.util.Date",
"2024-04-01 19:22:44"
],
"updateTime": [
"java.util.Date",
1718243055000
],
"deleted": false,
"username": "18947628476",
"password": null,
"nickName": "小弟",
"avatar": "https://cdn.apifox.com/app/project-icon/builtin/16.jpg",
"sex": null,
"phone": null,
"memo": null,
"openId": null,
"unionId": null,
"lastLoginIp": null,
"lastLoginTime": null,
"status": 1,
"salt": null
}