|
@@ -1,7 +1,10 @@
|
|
package io.github.nnkwrik.common.token.injection;
|
|
package io.github.nnkwrik.common.token.injection;
|
|
|
|
|
|
import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
import com.auth0.jwt.exceptions.TokenExpiredException;
|
|
|
|
+import com.google.common.cache.Cache;
|
|
|
|
+import com.google.common.cache.CacheBuilder;
|
|
import io.github.nnkwrik.common.dto.JWTUser;
|
|
import io.github.nnkwrik.common.dto.JWTUser;
|
|
|
|
+import io.github.nnkwrik.common.exception.JWTException;
|
|
import io.github.nnkwrik.common.token.TokenSolver;
|
|
import io.github.nnkwrik.common.token.TokenSolver;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -12,6 +15,8 @@ import org.springframework.web.context.request.NativeWebRequest;
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
|
import org.springframework.web.method.support.ModelAndViewContainer;
|
|
import org.springframework.web.method.support.ModelAndViewContainer;
|
|
|
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* @author nnkwrik
|
|
* @author nnkwrik
|
|
* @date 18/11/24 9:43
|
|
* @date 18/11/24 9:43
|
|
@@ -20,6 +25,9 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
|
@Component
|
|
@Component
|
|
public class JWTResolver implements HandlerMethodArgumentResolver {
|
|
public class JWTResolver implements HandlerMethodArgumentResolver {
|
|
|
|
|
|
|
|
+ private static Cache<String, JWTUser> cache =
|
|
|
|
+ CacheBuilder.newBuilder().maximumSize(10000).expireAfterWrite(3, TimeUnit.MINUTES).build();
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
private TokenSolver tokenSolver;
|
|
private TokenSolver tokenSolver;
|
|
|
|
|
|
@@ -35,19 +43,30 @@ public class JWTResolver implements HandlerMethodArgumentResolver {
|
|
NativeWebRequest webRequest,
|
|
NativeWebRequest webRequest,
|
|
WebDataBinderFactory binderFactory) {
|
|
WebDataBinderFactory binderFactory) {
|
|
String token = webRequest.getHeader("Authorization");
|
|
String token = webRequest.getHeader("Authorization");
|
|
|
|
+ JWTUser user = null;
|
|
|
|
+ boolean isExpired = false;
|
|
if (token == null) {
|
|
if (token == null) {
|
|
log.info("用户的Authorization头为空,无法获取jwt");
|
|
log.info("用户的Authorization头为空,无法获取jwt");
|
|
- return null;
|
|
|
|
|
|
+ } else if ((user = cache.getIfPresent(token)) == null) { //试图从缓存获取
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ user = tokenSolver.solve(token);
|
|
|
|
+ cache.put(token, user);
|
|
|
|
+ } catch (TokenExpiredException e) {
|
|
|
|
+ log.info("jwt已过期,过期时间:{}", e.getMessage());
|
|
|
|
+ isExpired = true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.info("jwt解析失败");
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- JWTUser user = null;
|
|
|
|
- try {
|
|
|
|
- user = tokenSolver.solve(token);
|
|
|
|
- } catch (TokenExpiredException e) {
|
|
|
|
- log.info("jwt已过期,过期时间:{}", e.getMessage());
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.info("jwt解析失败");
|
|
|
|
|
|
+
|
|
|
|
+ if (user == null &&
|
|
|
|
+ parameter.getParameterAnnotation(JWT.class).required()) {
|
|
|
|
+ if (isExpired) throw new JWTException(JWTException.TOKEN_IS_EXPIRED, "凭证已过期");
|
|
|
|
+ throw new JWTException(JWTException.TOKEN_IS_EMPTY, "用户的Authorization头错误,无法获取jwt");
|
|
}
|
|
}
|
|
log.info("jwt解析结果为:{}", user);
|
|
log.info("jwt解析结果为:{}", user);
|
|
|
|
+
|
|
return user;
|
|
return user;
|
|
}
|
|
}
|
|
|
|
|