|
@@ -1,11 +1,14 @@
|
|
package thyyxxk.webserver.utils;
|
|
package thyyxxk.webserver.utils;
|
|
|
|
|
|
|
|
+import cn.hutool.core.convert.Convert;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.jwt.JWT;
|
|
import cn.hutool.jwt.JWT;
|
|
import cn.hutool.jwt.JWTUtil;
|
|
import cn.hutool.jwt.JWTUtil;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
+import thyyxxk.webserver.config.exception.BizException;
|
|
|
|
+import thyyxxk.webserver.config.exception.ExceptionEnum;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -45,8 +48,16 @@ public class TokenUtil {
|
|
return JWTUtil.createToken(map, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
|
return JWTUtil.createToken(map, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean verifyToken(String token) {
|
|
|
|
- return JWTUtil.verify(token, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8));
|
|
|
|
|
|
+ public void verifyToken(String token) {
|
|
|
|
+ final JWT jwt = JWTUtil.parseToken(token);
|
|
|
|
+ long expire_time = Convert.toLong(jwt.getPayload("expire_time"));
|
|
|
|
+ long now = System.currentTimeMillis();
|
|
|
|
+ if (now > expire_time) {
|
|
|
|
+ throw new BizException(ExceptionEnum.TOKEN_EXPIRED, "token已过期");
|
|
|
|
+ }
|
|
|
|
+ if (!JWTUtil.verify(token, PRIVATE_KEY.getBytes(StandardCharsets.UTF_8))) {
|
|
|
|
+ throw new BizException(ExceptionEnum.TOKEN_NOT_EXIST);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public String getTokenUserId() {
|
|
public String getTokenUserId() {
|