增加websocket
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
|
||||
import com.onekeycall.videotablet.entity.DeviceInfo;
|
||||
import com.onekeycall.videotablet.entity.User;
|
||||
import com.onekeycall.videotablet.result.Result;
|
||||
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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class BindSnController {
|
||||
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private DeviceSnService deviceSnService;
|
||||
|
||||
@PostMapping("/bind_sn")
|
||||
public Result bindSn(
|
||||
@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);
|
||||
String userPhone = user.getPhone();
|
||||
|
||||
// 3. 校验 sn 是否存在
|
||||
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
|
||||
if (deviceInfo == null) {
|
||||
return Result.error().message("sn not found");
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(deviceInfo.getBindPhone())) {
|
||||
return Result.error().message("sn already bind");
|
||||
}
|
||||
|
||||
|
||||
|
||||
return Result.ok();
|
||||
}
|
||||
}
|
||||
@@ -5,15 +5,11 @@ 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 org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
@@ -37,15 +33,34 @@ public class LoginController {
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest registerRequest) {
|
||||
public Result registerUser(@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
|
||||
try {
|
||||
userService.registerUser(registerRequest.getUsername(), registerRequest.getPassword());
|
||||
return new ResponseEntity<>("User registered successfully", HttpStatus.CREATED);
|
||||
userService.registerUser(userId, password);
|
||||
return Result.ok().message("User registered successfully");
|
||||
} catch (RuntimeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result login(
|
||||
@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
|
||||
// 1. 创建认证令牌
|
||||
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userId, password);
|
||||
|
||||
// 2. 使用 AuthenticationManager 进行认证(核心步骤)
|
||||
Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
||||
|
||||
// 3. 认证成功后生成 JWT
|
||||
User userDetails = (User) authentication.getPrincipal();
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUserId(), deviceId);
|
||||
|
||||
// 4. 返回 Token
|
||||
return Result.ok().data(Collections.singletonMap("token", tokenPair.toMap()));
|
||||
}
|
||||
|
||||
@PostMapping("/phone_login")
|
||||
public Result phoneLogin(
|
||||
@RequestHeader("Device-ID") String deviceId,
|
||||
@@ -74,48 +89,6 @@ public class LoginController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<?> login(
|
||||
@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
|
||||
// 1. 创建认证令牌
|
||||
Authentication authenticationToken = new UsernamePasswordAuthenticationToken(userId, password);
|
||||
|
||||
// 2. 使用 AuthenticationManager 进行认证(核心步骤)
|
||||
Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
||||
|
||||
// 3. 认证成功后生成 JWT
|
||||
User userDetails = (User) authentication.getPrincipal();
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUserId(), deviceId);
|
||||
|
||||
// 4. 返回 Token
|
||||
return ResponseEntity.ok(Collections.singletonMap("token", tokenPair.toMap()));
|
||||
}
|
||||
|
||||
// 注册请求参数类
|
||||
public static class RegisterRequest {
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
// Getters and Setters
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/phone_register")
|
||||
public Result registerByPhone(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@@ -185,6 +158,9 @@ public class LoginController {
|
||||
return Result.error().message("verify key is expired");
|
||||
}
|
||||
}
|
||||
|
||||
// @PostMapping("/device_login")
|
||||
// public Result loginByDeviceSn(){
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.onekeycall.videotablet.controller;
|
||||
|
||||
import com.onekeycall.videotablet.handler.CustomWebSocketHandler;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/ws")
|
||||
public class WebSocketController {
|
||||
|
||||
private final CustomWebSocketHandler webSocketHandler;
|
||||
|
||||
public WebSocketController(CustomWebSocketHandler webSocketHandler) {
|
||||
this.webSocketHandler = webSocketHandler;
|
||||
}
|
||||
|
||||
@PostMapping("/broadcast")
|
||||
public ResponseEntity<String> broadcast(@RequestBody String message) {
|
||||
webSocketHandler.broadcast(message);
|
||||
return ResponseEntity.ok("广播发送成功");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user