用户模块:用户登录接口
This commit is contained in:
parent
f058c354bb
commit
ce52a2b2c2
@ -0,0 +1,30 @@
|
||||
package com.huangge1199.picture.constant;
|
||||
|
||||
/**
|
||||
* UserConstant
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 15:31:22
|
||||
*/
|
||||
public interface UserConstant {
|
||||
|
||||
/**
|
||||
* 用户登录态键
|
||||
*/
|
||||
String USER_LOGIN_STATE = "user_login";
|
||||
|
||||
// region 权限
|
||||
|
||||
/**
|
||||
* 默认角色
|
||||
*/
|
||||
String DEFAULT_ROLE = "user";
|
||||
|
||||
/**
|
||||
* 管理员角色
|
||||
*/
|
||||
String ADMIN_ROLE = "admin";
|
||||
|
||||
// endregion
|
||||
}
|
||||
|
@ -3,7 +3,9 @@ package com.huangge1199.picture.controller;
|
||||
import com.huangge1199.picture.common.R;
|
||||
import com.huangge1199.picture.exception.ErrorCode;
|
||||
import com.huangge1199.picture.exception.ThrowUtils;
|
||||
import com.huangge1199.picture.model.dto.user.UserLoginRequest;
|
||||
import com.huangge1199.picture.model.dto.user.UserRegisterRequest;
|
||||
import com.huangge1199.picture.model.vo.LoginUserVO;
|
||||
import com.huangge1199.picture.service.UserService;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -11,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 用户
|
||||
@ -37,5 +40,18 @@ public class UserController {
|
||||
long result = userService.userRegister(userAccount, userPassword, checkPassword);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
public R<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
|
||||
ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR);
|
||||
String userAccount = userLoginRequest.getUserAccount();
|
||||
String userPassword = userLoginRequest.getUserPassword();
|
||||
LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request);
|
||||
return R.ok(loginUserVO);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.huangge1199.picture.model.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* UserLoginRequest
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 15:24:45
|
||||
*/
|
||||
@Data
|
||||
public class UserLoginRequest implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3191241716373120793L;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String userPassword;
|
||||
}
|
||||
|
@ -0,0 +1,63 @@
|
||||
package com.huangge1199.picture.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 已登录用户视图(脱敏)
|
||||
*
|
||||
* @author huangge1199
|
||||
* @since 2025/6/30 15:27:08
|
||||
*/
|
||||
@Data
|
||||
public class LoginUserVO implements Serializable {
|
||||
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 用户简介
|
||||
*/
|
||||
private String userProfile;
|
||||
|
||||
/**
|
||||
* 用户角色:user/admin
|
||||
*/
|
||||
private String userRole;
|
||||
|
||||
/**
|
||||
* 编辑时间
|
||||
*/
|
||||
private Date editTime;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
@ -2,6 +2,9 @@ package com.huangge1199.picture.service;
|
||||
|
||||
import com.huangge1199.picture.model.entity.User;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.huangge1199.picture.model.vo.LoginUserVO;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author hyy
|
||||
@ -28,4 +31,22 @@ public interface UserService extends IService<User> {
|
||||
*/
|
||||
String getEncryptPassword(String userPassword);
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userAccount 用户账户
|
||||
* @param userPassword 用户密码
|
||||
* @param request 请求
|
||||
* @return 脱敏后的用户信息
|
||||
*/
|
||||
LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request);
|
||||
|
||||
/**
|
||||
* 获得脱敏后的登录用户信息
|
||||
*
|
||||
* @param user 用户
|
||||
* @return 脱敏后的登录用户信息
|
||||
*/
|
||||
LoginUserVO getLoginUserVO(User user);
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,30 @@
|
||||
package com.huangge1199.picture.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.huangge1199.picture.constant.UserConstant;
|
||||
import com.huangge1199.picture.exception.ErrorCode;
|
||||
import com.huangge1199.picture.exception.MyException;
|
||||
import com.huangge1199.picture.model.entity.User;
|
||||
import com.huangge1199.picture.model.enums.UserRoleEnum;
|
||||
import com.huangge1199.picture.model.vo.LoginUserVO;
|
||||
import com.huangge1199.picture.service.UserService;
|
||||
import com.huangge1199.picture.mapper.UserMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* @author hyy
|
||||
* @description 针对表【user(用户)】的数据库操作Service实现
|
||||
* @createDate 2025-06-30 10:27:45
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
implements UserService {
|
||||
@Override
|
||||
@ -64,6 +71,50 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
|
||||
return DigestUtils.md5DigestAsHex((salt + userPassword).getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) {
|
||||
// 1. 校验
|
||||
if (StrUtil.hasBlank(userAccount, userPassword)) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "参数为空");
|
||||
}
|
||||
if (userAccount.length() < 4) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "账号错误");
|
||||
}
|
||||
if (userPassword.length() < 8) {
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "密码错误");
|
||||
}
|
||||
// 2. 加密
|
||||
String encryptPassword = getEncryptPassword(userPassword);
|
||||
// 查询用户是否存在
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("userAccount", userAccount);
|
||||
queryWrapper.eq("userPassword", encryptPassword);
|
||||
User user = this.baseMapper.selectOne(queryWrapper);
|
||||
// 用户不存在
|
||||
if (user == null) {
|
||||
log.info("user login failed, userAccount cannot match userPassword");
|
||||
throw new MyException(ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
|
||||
}
|
||||
// 3. 记录用户的登录态
|
||||
request.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, user);
|
||||
return this.getLoginUserVO(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取脱敏类的用户信息
|
||||
*
|
||||
* @param user 用户
|
||||
* @return 脱敏后的用户信息
|
||||
*/
|
||||
@Override
|
||||
public LoginUserVO getLoginUserVO(User user) {
|
||||
if (user == null) {
|
||||
return null;
|
||||
}
|
||||
LoginUserVO loginUserVO = new LoginUserVO();
|
||||
BeanUtil.copyProperties(user, loginUserVO);
|
||||
return loginUserVO;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user