提交 b3c20a44 编写于 作者: yubinCloud's avatar yubinCloud

9-8 后端增加登录接口

上级 12c87209
package io.github.yubincloud.fairywiki.controller;
import io.github.yubincloud.fairywiki.dto.req.UserLoginReqDto;
import io.github.yubincloud.fairywiki.dto.req.UserQueryReqDto;
import io.github.yubincloud.fairywiki.dto.req.UserResetPwdReqDto;
import io.github.yubincloud.fairywiki.dto.req.UserSaveReqDto;
import io.github.yubincloud.fairywiki.dto.resp.ErrorCode;
import io.github.yubincloud.fairywiki.dto.resp.PageRespDto;
import io.github.yubincloud.fairywiki.dto.resp.RestfulModel;
import io.github.yubincloud.fairywiki.dto.resp.UserQueryRespDto;
import io.github.yubincloud.fairywiki.dto.resp.*;
import io.github.yubincloud.fairywiki.service.UserService;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
......@@ -47,4 +45,18 @@ public class UserController {
userService.resetPwd(userResetPwdReqDto);
return new RestfulModel<>(ErrorCode.SUCCESS, "", 0);
}
/**
* 用户登录接口
* @param userLoginReqDto 用户的用户名及密码
* @return 登录成功则返回该用户的信息,失败则返回登录失败提示
*/
@PostMapping("/login")
public RestfulModel<UserLoginRespDto> login(@RequestBody @Valid UserLoginReqDto userLoginReqDto) {
userLoginReqDto.setPassword(
DigestUtils.md5DigestAsHex(userLoginReqDto.getPassword().getBytes())
);
UserLoginRespDto userLoginRespDto = userService.login(userLoginReqDto);
return new RestfulModel<>(ErrorCode.SUCCESS, "", userLoginRespDto);
}
}
package io.github.yubincloud.fairywiki.dto.req;
import lombok.Data;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
@Data
public class UserLoginReqDto {
@NotEmpty(message = "【用户名】不能为空")
private String loginName;
@NotEmpty(message = "【密码】不能为空")
@Pattern(regexp = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$", message = "【密码】规则不正确")
private String password;
}
package io.github.yubincloud.fairywiki.dto.resp;
import lombok.Data;
@Data
public class UserLoginRespDto {
private Long id;
private String loginName;
private String name;
}
......@@ -6,6 +6,7 @@ package io.github.yubincloud.fairywiki.exception;
public enum BusinessExceptionCode {
USER_LOGIN_NAME_EXIST("登录名已存在"),
LOGIN_USER_ERROR("用户名不存在或密码错误"),
;
private String desc;
......
......@@ -4,9 +4,11 @@ import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.github.yubincloud.fairywiki.domain.User;
import io.github.yubincloud.fairywiki.domain.UserExample;
import io.github.yubincloud.fairywiki.dto.req.UserLoginReqDto;
import io.github.yubincloud.fairywiki.dto.req.UserResetPwdReqDto;
import io.github.yubincloud.fairywiki.dto.resp.PageRespDto;
import io.github.yubincloud.fairywiki.dto.resp.RestfulModel;
import io.github.yubincloud.fairywiki.dto.resp.UserLoginRespDto;
import io.github.yubincloud.fairywiki.exception.BusinessException;
import io.github.yubincloud.fairywiki.exception.BusinessExceptionCode;
import io.github.yubincloud.fairywiki.mapper.UserMapper;
......@@ -106,4 +108,28 @@ public class UserService {
User user = CopyUtil.copy(reqDto, User.class);
userMapper.updateByPrimaryKeySelective(user);
}
/**
* 对用户登录进行校验
* @param reqDto 用户登录请求的信息
* @return 若登录成功,则返回该用户的信息;失败则抛出 BusinessException
*/
public UserLoginRespDto login(UserLoginReqDto reqDto) {
User userInDb = selectByLoginName(reqDto.getLoginName());
if (ObjectUtils.isEmpty(userInDb)) {
// 用户名不存在
LOG.info("用户名不存在,{}", reqDto.getLoginName());
throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
} else {
if (userInDb.getPassword().equals(reqDto.getPassword())) {
// 登录成功
UserLoginRespDto userLoginDto = CopyUtil.copy(userInDb, UserLoginRespDto.class);
return userLoginDto;
} else {
// 密码不对
LOG.info("密码不对,输入密码:{}, 数据库密码:{}", reqDto.getPassword(), userInDb.getPassword());
throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册