登录和刷新token优化
This commit is contained in:
@@ -5,6 +5,8 @@ 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;
|
||||
@@ -26,6 +28,8 @@ public class LoginController {
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(LoginController.class);
|
||||
|
||||
@Autowired
|
||||
public LoginController(UserService userService, AuthenticationManager authenticationManager) {
|
||||
this.userService = userService;
|
||||
@@ -65,6 +69,8 @@ public class LoginController {
|
||||
public Result phoneLogin(
|
||||
@RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam String phone, @RequestParam String password) {
|
||||
logger.info("phoneLogin: phone={}, password={}, deviceId={}", phone, password, deviceId);
|
||||
|
||||
User user = userService.getUserByPhone(phone);
|
||||
if (user == null) {
|
||||
return Result.error().message("手机号未注册");
|
||||
@@ -93,7 +99,8 @@ public class LoginController {
|
||||
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);
|
||||
// }
|
||||
@@ -109,6 +116,7 @@ public class LoginController {
|
||||
}
|
||||
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<>();
|
||||
@@ -122,7 +130,8 @@ public class LoginController {
|
||||
return Result.error().message(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
return Result.error().message("verify key is expired");
|
||||
// return Result.error().message("verify key is expired");
|
||||
return Result.error().message("验证码已过期,请重新获取");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,6 +151,7 @@ public class LoginController {
|
||||
}
|
||||
try {
|
||||
User user = userService.loginByPhone(phone, code);
|
||||
logger.info("loginByPhoneCode: user={}", user);
|
||||
// 生成并返回JWT令牌(实际项目中需要实现JWT逻辑)
|
||||
TokenPair tokenPair = jwtUtil.generateTokenPair(user.getUserId(), deviceId);
|
||||
Map<String, Object> tokenMap = new HashMap<>();
|
||||
@@ -158,9 +168,11 @@ public class LoginController {
|
||||
return Result.error().message("verify key is expired");
|
||||
}
|
||||
}
|
||||
|
||||
// @PostMapping("/device_login")
|
||||
// public Result loginByDeviceSn(){
|
||||
//
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user