Compare commits
3 Commits
feature/co
...
4a2aff888a
| Author | SHA1 | Date | |
|---|---|---|---|
| 4a2aff888a | |||
| 0b0959fa80 | |||
|
|
4401a8a44a |
@@ -1,9 +1,12 @@
|
|||||||
package com.bao.dating.controller;
|
package com.bao.dating.controller;
|
||||||
|
|
||||||
|
import com.bao.dating.common.Result;
|
||||||
|
import com.bao.dating.common.ResultCode;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/user")
|
@RequestMapping("/user")
|
||||||
@@ -11,4 +14,14 @@ public class UserController {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserService userService;
|
private UserService userService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
*/
|
||||||
|
@PostMapping("/login")
|
||||||
|
public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
|
||||||
|
UserLoginVO userloginVO = userService.userLogin(userLoginDTO);
|
||||||
|
return Result.success(ResultCode.SUCCESS, "登录成功", userloginVO);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,16 @@
|
|||||||
package com.bao.dating.mapper;
|
package com.bao.dating.mapper;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface UserMapper {
|
public interface UserMapper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据用户名查询用户
|
||||||
|
*
|
||||||
|
* @param username 用户名
|
||||||
|
* @return 用户
|
||||||
|
*/
|
||||||
|
User getByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
14
src/main/java/com/bao/dating/pojo/dto/UserLoginDTO.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.bao.dating.pojo.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录数据传输对象
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class UserLoginDTO implements Serializable {
|
||||||
|
private String username;
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@@ -38,4 +38,8 @@ public class User implements Serializable {
|
|||||||
private LocalDateTime createdAt;
|
private LocalDateTime createdAt;
|
||||||
|
|
||||||
private LocalDateTime updatedAt;
|
private LocalDateTime updatedAt;
|
||||||
|
|
||||||
|
private String userEmail;
|
||||||
|
|
||||||
|
private String userPhone;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,13 @@
|
|||||||
package com.bao.dating.service;
|
package com.bao.dating.service;
|
||||||
|
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
|
/**
|
||||||
|
* 登录
|
||||||
|
* @param userLoginDTO 登录参数
|
||||||
|
* @return 登录结果
|
||||||
|
*/
|
||||||
|
UserLoginVO userLogin(UserLoginDTO userLoginDTO);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,52 @@
|
|||||||
package com.bao.dating.service.impl;
|
package com.bao.dating.service.impl;
|
||||||
|
|
||||||
import com.bao.dating.mapper.UserMapper;
|
import com.bao.dating.mapper.UserMapper;
|
||||||
|
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||||
|
import com.bao.dating.pojo.entity.User;
|
||||||
|
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||||
import com.bao.dating.service.UserService;
|
import com.bao.dating.service.UserService;
|
||||||
|
import com.bao.dating.util.JwtUtil;
|
||||||
|
import com.bao.dating.util.MD5Util;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserServiceImpl implements UserService {
|
public class UserServiceImpl implements UserService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserMapper userMapper;
|
private UserMapper userMapper;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserLoginVO userLogin(UserLoginDTO userLoginDTO) {
|
||||||
|
// 参数校验
|
||||||
|
if (userLoginDTO == null || userLoginDTO.getUsername() == null || userLoginDTO.getPassword() == null) {
|
||||||
|
throw new RuntimeException("用户名或密码不能为空");
|
||||||
|
}
|
||||||
|
// 查询用户
|
||||||
|
User user = userMapper.getByUsername(userLoginDTO.getUsername());
|
||||||
|
if (user == null){
|
||||||
|
throw new RuntimeException("用户不存在");
|
||||||
|
}
|
||||||
|
// 密码加密
|
||||||
|
boolean match = MD5Util.verifyWithSalt(
|
||||||
|
userLoginDTO.getPassword(),
|
||||||
|
user.getSalt(),
|
||||||
|
user.getPasswordHash()
|
||||||
|
);
|
||||||
|
// 密码校验
|
||||||
|
if (!match){
|
||||||
|
throw new RuntimeException("密码错误");
|
||||||
|
}
|
||||||
|
// 生成token
|
||||||
|
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||||
|
// 封装返回
|
||||||
|
UserLoginVO userLoginVO = new UserLoginVO();
|
||||||
|
userLoginVO.setUserId(user.getUserId());
|
||||||
|
userLoginVO.setNickname(user.getNickname());
|
||||||
|
userLoginVO.setToken(token);
|
||||||
|
return userLoginVO;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,19 @@ spring:
|
|||||||
username: root
|
username: root
|
||||||
password: JoyeeServe2025
|
password: JoyeeServe2025
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
|
redis:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
password: ""
|
||||||
|
database: 0
|
||||||
|
timeout: 10000
|
||||||
|
# 连接池配置(lettuce是Spring Boot默认Redis客户端,性能更优)
|
||||||
|
lettuce:
|
||||||
|
pool:
|
||||||
|
max-active: 8 # 连接池最大连接数(默认8,可根据业务并发调整)
|
||||||
|
max-wait: -1 # 连接池最大阻塞等待时间(毫秒,-1表示无限制)
|
||||||
|
max-idle: 8 # 连接池最大空闲连接数(默认8)
|
||||||
|
min-idle: 1 # 连接池最小空闲连接数(默认0,建议设置1-4,提高连接复用率)
|
||||||
# 邮箱SMTP配置
|
# 邮箱SMTP配置
|
||||||
mail:
|
mail:
|
||||||
host: smtp.163.com # QQ邮箱SMTP服务器地址
|
host: smtp.163.com # QQ邮箱SMTP服务器地址
|
||||||
|
|||||||
@@ -4,5 +4,8 @@
|
|||||||
|
|
||||||
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
<mapper namespace="com.bao.dating.mapper.UserMapper">
|
||||||
|
|
||||||
|
<select id="getByUsername" resultType="com.bao.dating.pojo.entity.User">
|
||||||
|
SELECT * FROM user WHERE user_name = #{userName}
|
||||||
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
Reference in New Issue
Block a user