修改登录逻辑,配合前后端联调

This commit is contained in:
spllzh
2025-08-11 19:31:54 +08:00
parent 2ab6f0c7d8
commit 895d6662d8
25 changed files with 208 additions and 22 deletions

View File

@@ -3,8 +3,14 @@ package com.rj;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//http://localhost:8080/swagger-ui/index.html?urls.primaryName=public-api
//http://101.43.230.106:8180/aismartcard/aicardbackend
/**
*
* http://localhost:9060/doc.html#/home
* http://localhost:9060/swagger-ui/index.html?urls.primaryName=public-api
* http://101.43.230.106:8180/aismartcard/aicardbackend
*
*/
@MapperScan("com.rj.mapper")
@SpringBootApplication
public class Langchain4jHeima20250803Application {

View File

@@ -305,3 +305,6 @@ public class MenuController {
}
}
}

View File

@@ -275,3 +275,6 @@ public class RoleController {
}
}
}

View File

@@ -2,7 +2,10 @@ package com.rj.controller.sys;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rj.common.PasswordUtil;
import com.rj.entity.sys.User;
import com.rj.pojo.sys.LoginRequest;
import com.rj.pojo.sys.LoginResponse;
import com.rj.service.sys.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
@@ -11,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -31,6 +35,28 @@ public class UserController {
@Autowired
private IUserService userService;
/**
* 用户登录
*/
@PostMapping("/login")
@Operation(summary = "用户登录", description = "根据用户名和密码进行登录验证")
public ResponseEntity<Map<String, Object>> login(
@Parameter(description = "登录信息", required = true)
@Valid @RequestBody LoginRequest loginRequest) {
Map<String, Object> result = new HashMap<>();
try {
LoginResponse loginResponse = userService.login(loginRequest);
result.put("success", true);
result.put("message", "登录成功");
result.put("data", loginResponse);
return ResponseEntity.ok(result);
} catch (Exception e) {
result.put("success", false);
result.put("message", "登录失败:" + e.getMessage());
return ResponseEntity.badRequest().body(result);
}
}
/**
* 新增用户
*/
@@ -41,6 +67,23 @@ public class UserController {
@RequestBody User user) {
Map<String, Object> result = new HashMap<>();
try {
// 检查用户名是否已存在
User existingUser = userService.getUserByUserName(user.getUserName());
if (existingUser != null) {
result.put("success", false);
result.put("message", "用户名已存在");
return ResponseEntity.badRequest().body(result);
}
// 保存原始密码并加密
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
// 保存原始密码
user.setOriginalPassword(user.getPassword());
// 对密码进行加密
String encryptedPassword = PasswordUtil.encryptPasswordWithDefaultSalt(user.getPassword());
user.setPassword(encryptedPassword);
}
boolean success = userService.save(user);
if (success) {
result.put("success", true);
@@ -176,6 +219,23 @@ public class UserController {
return ResponseEntity.badRequest().body(result);
}
// 检查用户名是否已被其他用户使用
User existingUser = userService.getUserByUserName(user.getUserName());
if (existingUser != null && !existingUser.getUserId().equals(user.getUserId())) {
result.put("success", false);
result.put("message", "用户名已被其他用户使用");
return ResponseEntity.badRequest().body(result);
}
// 如果密码不为空,则保存原始密码并加密
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
// 保存原始密码
user.setOriginalPassword(user.getPassword());
// 对密码进行加密
String encryptedPassword = PasswordUtil.encryptPasswordWithDefaultSalt(user.getPassword());
user.setPassword(encryptedPassword);
}
boolean success = userService.updateById(user);
if (success) {

View File

@@ -281,3 +281,6 @@ public class UserRoleController {
}
}
}

View File

@@ -41,6 +41,10 @@ public class User implements Serializable {
@TableField("password")
private String password;
@Schema(description = "原始密码")
@TableField("original_password")
private String originalPassword;
@Schema(description = "token令牌")
@TableField("token")
private String token;

View File

@@ -26,11 +26,11 @@ public class UserRole implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "用户编号")
@TableId("user_id")
@TableField("user_id")
private Integer userId;
@Schema(description = "角色编号")
@TableId("role_id")
@TableField("role_id")
private Integer roleId;
}

View File

@@ -7,6 +7,7 @@ CREATE TABLE `user` (
`user_name` varchar(255) DEFAULT NULL COMMENT '用户名称',
`email` varchar(255) DEFAULT NULL COMMENT '邮箱',
`password` varchar(255) DEFAULT NULL COMMENT '密码',
`original_password` varchar(255) DEFAULT NULL COMMENT '原始密码',
`token` varchar(255) DEFAULT NULL COMMENT 'token令牌',
`avatar` varchar(255) DEFAULT NULL COMMENT '头像',
PRIMARY KEY (`user_id`)

View File

@@ -14,3 +14,4 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface MenuMapper extends BaseMapper<Menu> {
}

View File

@@ -14,3 +14,4 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface RoleMapper extends BaseMapper<Role> {
}

View File

@@ -14,3 +14,4 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
}

View File

@@ -14,3 +14,4 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserRoleMapper extends BaseMapper<UserRole> {
}

View File

@@ -14,3 +14,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface IMenuService extends IService<Menu> {
}

View File

@@ -14,3 +14,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface IRoleService extends IService<Role> {
}

View File

@@ -14,3 +14,6 @@ import com.baomidou.mybatisplus.extension.service.IService;
public interface IUserRoleService extends IService<UserRole> {
}

View File

@@ -1,6 +1,8 @@
package com.rj.service.sys;
import com.rj.entity.sys.User;
import com.rj.pojo.sys.LoginRequest;
import com.rj.pojo.sys.LoginResponse;
import com.baomidou.mybatisplus.extension.service.IService;
/**
@@ -13,4 +15,32 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface IUserService extends IService<User> {
/**
* 用户登录
*
* @param loginRequest 登录请求
* @return 登录响应
*/
LoginResponse login(LoginRequest loginRequest);
/**
* 根据用户名查询用户
*
* @param userName 用户名
* @return 用户信息
*/
User getUserByUserName(String userName);
/**
* 根据token查询用户
*
* @param token 用户token
* @return 用户信息
*/
User getUserByToken(String token);
/**
* 通过token查询用户信息
*/
User getUserInfoByToken(String token);
}

View File

@@ -18,3 +18,6 @@ import org.springframework.stereotype.Service;
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
}

View File

@@ -18,3 +18,6 @@ import org.springframework.stereotype.Service;
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
}

View File

@@ -18,3 +18,6 @@ import org.springframework.stereotype.Service;
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
}

View File

@@ -1,11 +1,22 @@
package com.rj.service.sys.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rj.common.PasswordUtil;
import com.rj.entity.sys.User;
import com.rj.mapper.sys.UserMapper;
import com.rj.pojo.sys.LoginRequest;
import com.rj.pojo.sys.LoginResponse;
import com.rj.service.sys.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* <p>
* 用户表 服务实现类
@@ -17,4 +28,58 @@ import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
@Override
public LoginResponse login(LoginRequest loginRequest) {
// 根据用户名查询用户
User user = getUserByUserName(loginRequest.getUserName());
if (user == null) {
throw new RuntimeException("用户不存在");
}
// 验证密码
if (!PasswordUtil.verifyPassword(loginRequest.getPassword(), user.getPassword())) {
throw new RuntimeException("密码错误");
}
// 生成token
String token = UUID.randomUUID().toString().replace("-", "");
// 更新用户的token
user.setToken(token);
this.updateById(user);
// 构建登录响应
LoginResponse loginResponse = new LoginResponse();
loginResponse.setUserId(user.getUserId());
loginResponse.setUserName(user.getUserName());
loginResponse.setEmail(user.getEmail());
loginResponse.setAvatar(user.getAvatar());
loginResponse.setToken(token);
loginResponse.setLoginTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
return loginResponse;
}
@Override
public User getUserByUserName(String userName) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getUserName, userName);
return this.getOne(queryWrapper);
}
@Override
public User getUserByToken(String token) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getToken, token);
return this.getOne(queryWrapper);
}
@Override
public User getUserInfoByToken(String token) {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getToken, token);
return this.getOne(queryWrapper);
}
}

View File

@@ -34,9 +34,9 @@ logging:
# MyBatis SQL日志
com.rj.mapper: DEBUG
# 数据库连接池日志
com.zaxxer.hikari: DEBUG
# com.zaxxer.hikari: DEBUG
# Spring JDBC日志
org.springframework.jdbc: DEBUG
# org.springframework.jdbc: DEBUG
# 显示SQL参数
com.baomidou.mybatisplus.core.executor: DEBUG
# 显示SQL执行时间

View File

@@ -19,3 +19,6 @@
</sql>
</mapper>

View File

@@ -14,3 +14,4 @@
</sql>
</mapper>

View File

@@ -18,3 +18,4 @@
</sql>
</mapper>

View File

@@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rj.mapper.sys.UserRoleMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.rj.entity.sys.UserRole">
<id column="user_id" property="userId" />
<id column="role_id" property="roleId" />
</resultMap>
<!-- 通用查询结果列 -->
<sql id="Base_Column_List">
user_id, role_id
</sql>
</mapper>