增加websocket
This commit is contained in:
@@ -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(){
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user