|
|
@@ -12,6 +12,8 @@ import org.springframework.http.HttpMethod;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
import static com.roma.romaapi.utils.JWTUtil.REDIS_USER_EXPIRE_TIME;
|
|
|
import static com.roma.romaapi.utils.JWTUtil.SIGN;
|
|
|
|
|
|
@@ -35,24 +37,25 @@ public class JWTInterceptor implements HandlerInterceptor {
|
|
|
if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
|
|
|
return true;
|
|
|
}
|
|
|
- // 从Header中获得Token
|
|
|
- String token = request.getHeader("token");
|
|
|
- String aa = request.getHeader("Authorization");
|
|
|
- System.out.println(aa+"================");
|
|
|
- if(token==null || token.equals("")){
|
|
|
+ String authorization = request.getHeader("Authorization");
|
|
|
+ if(authorization==null || authorization.equals("")){
|
|
|
+ throw new Exception("Header 未装载 token");
|
|
|
+ }
|
|
|
+ String token = authorization.substring(7);
|
|
|
+ if(token.equals("")){
|
|
|
throw new Exception("Header 未装载 token");
|
|
|
}
|
|
|
try {
|
|
|
// 得到签名实体
|
|
|
DecodedJWT verify = jwtUtil.verify(token);
|
|
|
-
|
|
|
// 得到签名中的登录时间
|
|
|
String loginTimeFromToken = verify.getClaim("userLoginTime").asString();
|
|
|
-
|
|
|
- System.out.println("token-----"+loginTimeFromToken);
|
|
|
-
|
|
|
+ // 续期
|
|
|
+ String userId = stringRedisTemplate.opsForValue().get(SIGN + token);
|
|
|
+ String redisTokenKey = SIGN + token;
|
|
|
+ stringRedisTemplate.opsForValue().set(redisTokenKey, userId, 60*30, TimeUnit.SECONDS);
|
|
|
} catch (SignatureVerificationException e) {
|
|
|
-
|
|
|
+ System.out.println("token签名错误-----"+e.getMessage());
|
|
|
throw new Exception("无效Token签名");
|
|
|
} catch (TokenExpiredException e) {
|
|
|
/*若抛出token过期异常,检查redis中的是否存在token以及请求头中的token与redis中的token是否相同
|
|
|
@@ -60,11 +63,11 @@ public class JWTInterceptor implements HandlerInterceptor {
|
|
|
// 从Redis中获取缓存中的token,判断是否过期
|
|
|
String userId = stringRedisTemplate.opsForValue().get(SIGN + token);
|
|
|
if(null == userId || userId.equals("")){
|
|
|
- throw new Exception("Original Token 无效或已过期");
|
|
|
+ throw new Exception("拦截器 Original Token 无效或已过期");
|
|
|
} else {
|
|
|
// 续期
|
|
|
String redisTokenKey = SIGN + token;
|
|
|
- stringRedisTemplate.opsForValue().set(redisTokenKey, userId, REDIS_USER_EXPIRE_TIME);
|
|
|
+ stringRedisTemplate.opsForValue().set(redisTokenKey, userId, 60*30, TimeUnit.SECONDS);
|
|
|
|
|
|
return true;
|
|
|
}
|