分离注册登录接口
This commit is contained in:
@@ -20,6 +20,8 @@ import java.util.*;
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class LoginController {
|
||||
Logger logger = LoggerFactory.getLogger(LoginController.class);
|
||||
|
||||
|
||||
private final UserService userService;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
@@ -29,7 +31,6 @@ public class LoginController {
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(LoginController.class);
|
||||
|
||||
@Autowired
|
||||
public LoginController(UserService userService, AuthenticationManager authenticationManager) {
|
||||
@@ -37,17 +38,6 @@ public class LoginController {
|
||||
this.authenticationManager = authenticationManager;
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public Result registerUser(@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
|
||||
try {
|
||||
userService.registerUser(userId, password);
|
||||
return Result.ok().message("User registered successfully");
|
||||
} catch (RuntimeException e) {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public Result login(
|
||||
@RequestHeader("Device-ID") String deviceId,
|
||||
@@ -59,8 +49,8 @@ public class LoginController {
|
||||
Authentication authentication = authenticationManager.authenticate(authenticationToken);
|
||||
|
||||
// 3. 认证成功后生成 JWT
|
||||
User userDetails = (User) authentication.getPrincipal();
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(userDetails.getUserId(), deviceId);
|
||||
User user = (User) authentication.getPrincipal();
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
|
||||
// 4. 返回 Token
|
||||
return Result.ok().data(Collections.singletonMap("token", tokenPair.toMap()));
|
||||
@@ -100,50 +90,10 @@ public class LoginController {
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/phone_register")
|
||||
public Result registerByPhone(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
|
||||
logger.info("registerByPhone: phone={}, code={}, verifyKey={}, deviceId={}", phone, code, verifyKey, deviceId);
|
||||
|
||||
// if (TextUtils.isEmpty(verifyKey)) {
|
||||
// return Result.error().message("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
// }
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String redisVerifyKey = (String) map.get("verifyKey");
|
||||
if (!Objects.equals(redisVerifyKey, verifyKey)) {
|
||||
return Result.error().message("verify key is not same");
|
||||
}
|
||||
String redisCode = map.get("code").toString();
|
||||
if (!Objects.equals(redisCode, code)) {
|
||||
return Result.error().message("code is not same");
|
||||
}
|
||||
try {
|
||||
User user = userService.registerByPhone(phone, code, deviceId, new Date());
|
||||
logger.info("loginByPhoneCode: user={}", user.toString());
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
//返回给app保存,access_token用来加入header请求接口,refresh_token用来更换access_token
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
tokenMap.put("new_user", user.isNewUser());
|
||||
tokenMap.put("user_id", user.getUserId());
|
||||
tokenMap.put("has_password", user.isHasPassword());
|
||||
tokenMap.put("token", tokenPair.toMap());
|
||||
redisTemplate.delete(phone);
|
||||
return Result.ok().data(tokenMap);
|
||||
} catch (RuntimeException e) {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
// return Result.error().message("verify key is expired");
|
||||
return Result.error().message("验证码已过期,请重新获取");
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/phone_code_login")
|
||||
public Result loginByPhoneCode(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "device_id") String deviceId) {
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "Device-ID") String deviceId) {
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String redisVerifyKey = (String) map.get("verifyKey");
|
||||
@@ -174,10 +124,4 @@ public class LoginController {
|
||||
}
|
||||
}
|
||||
|
||||
// @PostMapping("/device_login")
|
||||
// public Result loginByDeviceSn(){
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
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 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.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class RegisterController {
|
||||
Logger logger = LoggerFactory.getLogger(RegisterController.class);
|
||||
|
||||
|
||||
private final UserService userService;
|
||||
private final AuthenticationManager authenticationManager;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
public RegisterController(UserService userService, AuthenticationManager authenticationManager) {
|
||||
this.userService = userService;
|
||||
this.authenticationManager = authenticationManager;
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public Result registerUser(@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
|
||||
try {
|
||||
userService.registerUser(userId, password);
|
||||
return Result.ok().message("User registered successfully");
|
||||
} catch (RuntimeException e) {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/phone_register")
|
||||
public Result registerByPhone(
|
||||
@RequestParam String phone, @RequestParam String code,
|
||||
@RequestParam(value = "verify_key") String verifyKey, @RequestParam(value = "Device-ID") String deviceId) {
|
||||
logger.info("registerByPhone: phone={}, code={}, verifyKey={}, deviceId={}", phone, code, verifyKey, deviceId);
|
||||
|
||||
// if (TextUtils.isEmpty(verifyKey)) {
|
||||
// return Result.error().message("verify key is empty", HttpStatus.BAD_REQUEST);
|
||||
// }
|
||||
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
||||
if (map != null) {
|
||||
String redisVerifyKey = (String) map.get("verifyKey");
|
||||
if (!Objects.equals(redisVerifyKey, verifyKey)) {
|
||||
return Result.error().message("verify key is not same");
|
||||
}
|
||||
String redisCode = map.get("code").toString();
|
||||
if (!Objects.equals(redisCode, code)) {
|
||||
return Result.error().message("code is not same");
|
||||
}
|
||||
try {
|
||||
User user = userService.registerByPhone(phone, code, deviceId, new Date());
|
||||
logger.info("loginByPhoneCode: user={}", user.toString());
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
//返回给app保存,access_token用来加入header请求接口,refresh_token用来更换access_token
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
tokenMap.put("new_user", user.isNewUser());
|
||||
tokenMap.put("user_id", user.getUserId());
|
||||
tokenMap.put("has_password", user.isHasPassword());
|
||||
tokenMap.put("token", tokenPair.toMap());
|
||||
redisTemplate.delete(phone);
|
||||
return Result.ok().data(tokenMap);
|
||||
} catch (RuntimeException e) {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
// return Result.error().message("verify key is expired");
|
||||
return Result.error().message("验证码已过期,请重新获取");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user