登录和刷新token优化
This commit is contained in:
@@ -8,14 +8,27 @@ import com.onekeycall.videotablet.service.UserService;
|
||||
import com.onekeycall.videotablet.utils.JwtUtil;
|
||||
import com.onekeycall.videotablet.utils.PushUtils;
|
||||
import com.onekeycall.videotablet.utils.TextUtils;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import io.jsonwebtoken.JwtException;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.util.Base64;
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/public")
|
||||
@RequestMapping("/sn")
|
||||
public class BindSnController {
|
||||
|
||||
@Autowired
|
||||
@@ -25,17 +38,27 @@ public class BindSnController {
|
||||
@Autowired
|
||||
private DeviceSnService deviceSnService;
|
||||
|
||||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
/**
|
||||
* 用户app发送绑定推送到手机
|
||||
*
|
||||
* @param authHeader
|
||||
* @param deviceId
|
||||
* @param userId
|
||||
* @param sn
|
||||
* @return
|
||||
*/
|
||||
@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");
|
||||
}
|
||||
@@ -43,7 +66,6 @@ public class BindSnController {
|
||||
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");
|
||||
@@ -55,9 +77,10 @@ public class BindSnController {
|
||||
|
||||
|
||||
try {
|
||||
String randomString = RandomStringUtils.randomAlphanumeric(32);
|
||||
// PushUtils.aliyunAsyncPush(randomString, userPhone, sn);
|
||||
PushUtils.tpnsPush(randomString, userPhone, sn);
|
||||
String verifyKey = RandomStringUtils.randomAlphanumeric(32);
|
||||
// PushUtils.aliyunAsyncPush(verifyKey, userPhone, sn);
|
||||
PushUtils.tpnsPush(verifyKey, userPhone, sn);
|
||||
redisTemplate.opsForValue().set(sn, verifyKey, 1, TimeUnit.MINUTES);
|
||||
return Result.ok().message("send message success");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
@@ -66,41 +89,63 @@ public class BindSnController {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 平板根据返回的数据绑定手机
|
||||
*
|
||||
* @param authHeader
|
||||
* @param deviceId
|
||||
* @param userId
|
||||
* @param sn
|
||||
* @param verifyKey
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/device_bind")
|
||||
public Result deviceBind(
|
||||
@RequestHeader("Authorization") String authHeader, @RequestHeader("Device-ID") String deviceId,
|
||||
@RequestParam(value = "user_id") String userId, @RequestParam(value = "sn") String sn,
|
||||
@RequestParam(value = "verify_key") String verifyKey) {
|
||||
// 1. 校验 Authorization 头
|
||||
|
||||
String redisVerifyKey = (String) redisTemplate.opsForValue().get(sn);
|
||||
if (redisVerifyKey == null) {
|
||||
return Result.notFound().message("verify key not found");
|
||||
}
|
||||
if (!Objects.equals(redisVerifyKey, verifyKey)) {
|
||||
return Result.error().message("verify key is not same");
|
||||
}
|
||||
|
||||
if (!authHeader.startsWith("Bearer ")) {
|
||||
return Result.error().message("Invalid Authorization header");
|
||||
return Result.unAuthorized().message("Invalid Authorization header");
|
||||
}
|
||||
String token = authHeader.substring(7); // 去掉 "Bearer " 前缀
|
||||
|
||||
// 2. 校验 Token
|
||||
if (!jwtUtil.validateAccessToken(userId, token, deviceId)) {
|
||||
return Result.error().message("Invalid token");
|
||||
return Result.unAuthorized().message("Invalid token");
|
||||
}
|
||||
|
||||
User user = userService.getUserByUserId(userId);
|
||||
if (user == null) {
|
||||
return Result.error().message("user not found");
|
||||
return Result.notFound().message("user not found");
|
||||
}
|
||||
|
||||
String userPhone = user.getPhone();
|
||||
|
||||
|
||||
// 3. 校验 sn 是否存在
|
||||
DeviceInfo oldDeviceInfo = deviceSnService.findBySn(sn);
|
||||
if (oldDeviceInfo == null) {
|
||||
return Result.error().message("sn not found");
|
||||
return Result.notFound().message("sn not found");
|
||||
}
|
||||
|
||||
if (!TextUtils.isEmpty(oldDeviceInfo.getBindPhone())) {
|
||||
return Result.error().message("sn already bind");
|
||||
}
|
||||
|
||||
String deviceSig = jwtUtil.generateDeviceSig(sn);
|
||||
String deviceToken = jwtUtil.generateDeviceToken(sn, deviceId);
|
||||
|
||||
oldDeviceInfo.setBindPhone(userPhone);
|
||||
oldDeviceInfo.setDeviceAlias(user.getNickname() + "的平板");
|
||||
oldDeviceInfo.setBindTime(new Date());
|
||||
oldDeviceInfo.setDeviceModel(deviceId);
|
||||
oldDeviceInfo.setBindSig(deviceSig);
|
||||
oldDeviceInfo.setToken(deviceToken);
|
||||
oldDeviceInfo.setSn(sn);
|
||||
deviceSnService.save(oldDeviceInfo);
|
||||
|
||||
@@ -113,4 +158,32 @@ public class BindSnController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取平板sn绑定状态
|
||||
*
|
||||
* @param Device_Token
|
||||
* @param deviceId
|
||||
* @param Device_Sig
|
||||
* @param sn
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/get_bind_statu")
|
||||
public Result getBindStatus(
|
||||
@RequestHeader("Device_Token") String Device_Token, @RequestHeader("Device-ID") String deviceId,
|
||||
@RequestHeader("Device_Sig") String Device_Sig, @RequestParam(value = "sn") String sn) {
|
||||
|
||||
|
||||
DeviceInfo deviceInfo = deviceSnService.findBySn(sn);
|
||||
if (deviceInfo == null) {
|
||||
return Result.notFound().message("sn not found");
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(deviceInfo.getBindPhone())) {
|
||||
return Result.error().message("sn not bind");
|
||||
}
|
||||
|
||||
return Result.ok().message("sn bind");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user