优化统一鉴权,优化目录结构

This commit is contained in:
2025-09-08 11:32:33 +08:00
parent 9f3b18f2df
commit a33eeef27e
22 changed files with 487 additions and 122 deletions

View File

@@ -0,0 +1,188 @@
package com.onekeycall.videotablet.controller.user;
import com.onekeycall.videotablet.controller.pub.LoginController;
import com.onekeycall.videotablet.dto.TokenPair;
import com.onekeycall.videotablet.entity.DeviceApkInfo;
import com.onekeycall.videotablet.entity.DeviceInfo;
import com.onekeycall.videotablet.entity.DeviceLocation;
import com.onekeycall.videotablet.entity.User;
import com.onekeycall.videotablet.result.Result;
import com.onekeycall.videotablet.service.DeviceApkInfoService;
import com.onekeycall.videotablet.service.DeviceLocationService;
import com.onekeycall.videotablet.service.DeviceSnService;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.JwtUtil;
import com.onekeycall.videotablet.utils.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserService userService;
private final AuthenticationManager authenticationManager;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
private JwtUtil jwtUtil;
@Autowired
private DeviceSnService deviceSnService;
@Autowired
private DeviceLocationService deviceLocationService;
@Autowired
private DeviceApkInfoService deviceApkInfoService;
Logger logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
public UserController(UserService userService, AuthenticationManager authenticationManager) {
this.userService = userService;
this.authenticationManager = authenticationManager;
}
@PostMapping("/refresh_token")
public Result refreshToken(
@RequestHeader(value = "Authorization", required = false) String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam("refresh_token") String refreshToken) {
logger.info("refreshToken: Authorization={} userId={} deviceId={} refreshToken={}", authHeader, userId, deviceId, refreshToken);
try {
// 验证refreshToken的有效性
if (!jwtUtil.validateRefreshToken(refreshToken, userId)) {
return Result.error().message("无效的refresh token");
}
// 从refreshToken中获取用户ID
TokenPair tokenPair = jwtUtil.refreshAccessToken(refreshToken, deviceId);
// 构建返回结果
Map<String, Object> tokenMap = new HashMap<>();
tokenMap.put("access_token", tokenPair.getAccess_token());
return Result.ok().data(tokenMap);
} catch (Exception e) {
logger.error("刷新token失败", e);
return Result.error().message("刷新token失败: " + e.getMessage());
}
}
@GetMapping("/get_user_info")
public Result getUserInfo(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId
) {
// 1. 校验 Authorization 头
if (!authHeader.startsWith("Bearer ")) {
return Result.error().message("Invalid Authorization header");
}
String token = authHeader.substring(7); // 去掉 "Bearer " 前缀
// 2. 校验 Token
if (!jwtUtil.validateAccessToken(userId, token, deviceId)) {
return Result.error().message("Invalid token");
}
User user = userService.getUserByUserId(userId);
Map<String, Object> userInfo = new HashMap<>();
userInfo.put("user_id", user.getUserId());
userInfo.put("phone", user.getPhone());
userInfo.put("nickname", user.getNickname());
userInfo.put("avatar", user.getAvatar());
userInfo.put("set_password", !TextUtils.isEmpty(user.getPassword()));
return Result.ok().data("user_info", userInfo);
}
@GetMapping("/get_sn_list")
public Result register(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn", required = false) String sn) {
// 1. 校验 Authorization 头
if (!authHeader.startsWith("Bearer ")) {
return Result.error().message("Invalid Authorization header");
}
String token = authHeader.substring(7); // 去掉 "Bearer " 前缀
// 2. 校验 Token
if (!jwtUtil.validateAccessToken(userId, token, deviceId)) {
return Result.error().message("Invalid token");
}
if (TextUtils.isEmpty(sn)) {
List<DeviceInfo> deviceInfos = deviceSnService.findByUserId(userId);
if (deviceInfos == null || deviceInfos.isEmpty()) {
return Result.notFound().message("sn not found");
} else {
return Result.ok().data("deviceInfos", deviceInfos);
}
} else {
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
if (deviceInfo == null) {
return Result.notFound().message("sn not found");
}
if (!deviceInfo.getUserId().equals(userId)) {
return Result.error().message("sn not belong to user");
}
return Result.ok().data("deviceInfo", deviceInfo);
}
}
@GetMapping("/get_sn_location")
public Result getSnLocation(
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn
) {
// 1. 校验 Authorization 头
if (!authHeader.startsWith("Bearer ")) {
return Result.error().message("Invalid Authorization header");
}
String token = authHeader.substring(7); // 去掉 "Bearer " 前缀
// 2. 校验 Token
if (!jwtUtil.validateAccessToken(userId, token, deviceId)) {
return Result.error().message("Invalid token");
}
User user = userService.getUserByUserId(userId);
if (user == null) {
return Result.error().message("User not found");
}
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
if (deviceInfo == null) {
return Result.error().message("Device not found");
}
if (!deviceInfo.getUserId().equals(userId)) {
return Result.error().message("Device not belong to user");
}
DeviceLocation deviceLocation = deviceLocationService.getDeviceLocation(sn);
if (deviceLocation == null) {
return Result.error().message("Device location not found");
}
return Result.ok().data("device_location", deviceLocation);
}
@GetMapping("/get_device_apk_list")
public Result getDeviceApkList(@RequestParam String sn) {
DeviceApkInfo deviceApkInfo = deviceApkInfoService.getDeviceApkInfoBySn(sn);
return Result.ok().data("deviceApkInfo", deviceApkInfo);
}
}