优化统一鉴权,优化目录结构
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.pub;
|
||||
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class HelloController {
|
||||
//引入 redis
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
@GetMapping("/public/hello")
|
||||
@GetMapping("/hello")
|
||||
public Result getMethodName() {
|
||||
return Result.ok().message("Welcome to Yijiantong");
|
||||
}
|
||||
@@ -24,7 +22,7 @@ public class HelloController {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/public/set")
|
||||
@PostMapping("/set")
|
||||
public Result setRedis(@RequestParam(value = "username") String username) {
|
||||
//存储 key-value 键值对: "username"-"jaychou"
|
||||
stringRedisTemplate.opsForValue().set("username", username);
|
||||
@@ -36,7 +34,7 @@ public class HelloController {
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/public/get")
|
||||
@GetMapping("/get")
|
||||
public Result getRedis(@RequestParam(value = "username") String username) {
|
||||
//通过 key 值读取 value
|
||||
String result = stringRedisTemplate.opsForValue().get(username);
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.pub;
|
||||
|
||||
import com.onekeycall.videotablet.dto.TokenPair;
|
||||
import com.onekeycall.videotablet.entity.User;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
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;
|
||||
@@ -77,6 +78,10 @@ public class LoginController {
|
||||
}
|
||||
String userId = user.getUserId();
|
||||
|
||||
if (TextUtils.isEmpty(user.getPassword())) {
|
||||
return Result.error().message("用户未设置密码,请使用验证码登录");
|
||||
}
|
||||
|
||||
// 1. 创建认证令牌
|
||||
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userId, password);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.pub;
|
||||
|
||||
import com.onekeycall.videotablet.config.FilePath;
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.sms;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.sms;
|
||||
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.sms.SendSms;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.sn;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
@@ -160,22 +160,22 @@ public class BindSnController {
|
||||
// TODO: 2025/8/22 Device_Token在docker无法被接收到,使用Device-Token代替
|
||||
@GetMapping("/get_bind_statu")
|
||||
public Result getBindStatus(
|
||||
@RequestHeader("Device-Token") String deviceToken, @RequestHeader("Device-ID") String deviceId,
|
||||
@RequestHeader("Device-Sig") String deviceSig,
|
||||
// @RequestHeader("Device-Token") String deviceToken, @RequestHeader("Device-ID") String deviceId,
|
||||
// @RequestHeader("Device-Sig") String deviceSig,
|
||||
@RequestParam(value = "sn") String sn) {
|
||||
|
||||
if (!jwtUtil.validateDeviceToken(deviceToken, deviceId, sn)) {
|
||||
return Result.error().message("Invalid token");
|
||||
}
|
||||
// if (!jwtUtil.validateDeviceToken(deviceToken, deviceId, sn)) {
|
||||
// return Result.error().message("Invalid token");
|
||||
// }
|
||||
|
||||
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
|
||||
if (deviceInfo == null) {
|
||||
return Result.notFound().message("sn not found");
|
||||
}
|
||||
|
||||
if (!deviceInfo.getBindSig().equals(deviceSig)) {
|
||||
return Result.error().message("device sig not match");
|
||||
}
|
||||
// if (!deviceInfo.getBindSig().equals(deviceSig)) {
|
||||
// return Result.error().message("device sig not match");
|
||||
// }
|
||||
|
||||
if (TextUtils.isEmpty(deviceInfo.getBindPhone())) {
|
||||
return Result.error().message("sn not bind");
|
||||
@@ -1,12 +1,8 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.sn;
|
||||
|
||||
import com.aliyun.core.annotation.Body;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.onekeycall.videotablet.bean.ApkUploadRequest;
|
||||
import com.onekeycall.videotablet.entity.ApkInfo;
|
||||
import com.onekeycall.videotablet.entity.DeviceApkInfo;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.DeviceApkInfoService;
|
||||
import org.slf4j.Logger;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.sn;
|
||||
|
||||
import com.onekeycall.videotablet.entity.Contact;
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
@@ -14,9 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/sn")
|
||||
@@ -30,41 +28,7 @@ public class DevicesController {
|
||||
@Autowired
|
||||
private ContactService contactService;
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/update_location")
|
||||
public Result updateLocation(
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.user;
|
||||
|
||||
import com.nimbusds.openid.connect.sdk.claims.UserInfo;
|
||||
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;
|
||||
@@ -21,6 +21,7 @@ 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
|
||||
@@ -105,6 +106,42 @@ public class UserController {
|
||||
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,
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
package com.onekeycall.videotablet.controller.user;
|
||||
|
||||
import com.onekeycall.videotablet.entity.User;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
import com.onekeycall.videotablet.service.UserService;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import com.onekeycall.videotablet.utils.TextUtils;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
Reference in New Issue
Block a user