登录和刷新token优化
This commit is contained in:
@@ -5,18 +5,24 @@ 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.CXAESUtil;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import com.onekeycall.videotablet.utils.PushUtils;
|
||||
import com.onekeycall.videotablet.utils.TextUtils;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
public class ManageSnController {
|
||||
Logger logger = LoggerFactory.getLogger(ManageSnController.class);
|
||||
|
||||
@Autowired
|
||||
private JwtUtil jwtUtil;
|
||||
@@ -50,8 +56,46 @@ public class ManageSnController {
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
deviceInfo.setSn(sn);
|
||||
deviceInfo.setAddTime(new Date());
|
||||
deviceInfo.setDeviceModel(deviceId);
|
||||
deviceSnService.save(deviceInfo);
|
||||
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
@GetMapping("/decode_sn")
|
||||
public Result decodeSn(
|
||||
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam(value = "encrypt_sn") String encryptSn) throws Exception {
|
||||
|
||||
logger.info("Authorization = {}, Device-ID = {}, user_id = {}, encrypt_sn = {}", authHeader, deviceId, userId, encryptSn);
|
||||
// 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");
|
||||
}
|
||||
|
||||
// 3. 解密 sn
|
||||
String sn = CXAESUtil.decrypt(CXAESUtil.key, encryptSn);
|
||||
logger.info("sn = {}", sn);
|
||||
if (TextUtils.isEmpty(sn)) {
|
||||
return Result.error().message("sn decrypt failed");
|
||||
}
|
||||
|
||||
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
|
||||
if (deviceInfo == null) {
|
||||
return Result.error().message("sn not found");
|
||||
}
|
||||
if (!TextUtils.isEmpty(deviceInfo.getBindPhone())) {
|
||||
return Result.ok().message("sn already bind");
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("sn", sn);
|
||||
|
||||
return Result.ok().data(map);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user