From 591c6f8561fd1dcb5d4b153828b44928bd77ecc1 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Wed, 6 May 2020 16:32:45 +0800 Subject: [PATCH] =?UTF-8?q?OCT=20REM:=201.=20=E6=9B=B4=E6=94=B9token?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C=E9=85=8D=E7=BD=AE=E9=A1=B9=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dispose/common/ConstValue.java | 4 ---- .../java/com/dispose/common/GlobalVar.java | 5 +++++ .../java/com/dispose/config/TokenConfig.java | 14 ++++++++++++ .../dispose/interceptor/TokenInterceptor.java | 22 +++++++++++++++++-- .../service/impl/UserAccountServiceImpl.java | 3 ++- .../dispose/Global/InitTestEnvironment.java | 2 ++ 6 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/dispose/common/ConstValue.java b/src/main/java/com/dispose/common/ConstValue.java index 6366fe38..45c74ced 100644 --- a/src/main/java/com/dispose/common/ConstValue.java +++ b/src/main/java/com/dispose/common/ConstValue.java @@ -21,10 +21,6 @@ public class ConstValue { * The constant IS_SKIP_TIMEOUT_CHECK. */ public static final boolean IS_SKIP_TIMEOUT_CHECK = true; - /** - * The constant IS_VERIFY_TOKEN. - */ - public static final boolean IS_VERIFY_TOKEN = true; /** * The constant TOKEN_EXPIRED_TIME_MS. */ diff --git a/src/main/java/com/dispose/common/GlobalVar.java b/src/main/java/com/dispose/common/GlobalVar.java index f69e15de..0985dc9e 100644 --- a/src/main/java/com/dispose/common/GlobalVar.java +++ b/src/main/java/com/dispose/common/GlobalVar.java @@ -30,4 +30,9 @@ public class GlobalVar { * The constant IS_CHECK_REQUEST_TIMEOUT. */ public static volatile boolean IS_CHECK_REQUEST_TIMEOUT = false; + + /** + * The constant IS_VERIFY_TOKEN. + */ + public static boolean IS_VERIFY_TOKEN = true; } diff --git a/src/main/java/com/dispose/config/TokenConfig.java b/src/main/java/com/dispose/config/TokenConfig.java index 7eb43c2b..195b1407 100644 --- a/src/main/java/com/dispose/config/TokenConfig.java +++ b/src/main/java/com/dispose/config/TokenConfig.java @@ -6,15 +6,29 @@ import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +/** + * The type Token config. + */ @Configuration public class TokenConfig implements WebMvcConfigurer { + /** + * Init auth interceptor token interceptor. + * + * @return the token interceptor + */ @Bean public TokenInterceptor initAuthInterceptor(){ return new TokenInterceptor(); } + /** + * Add interceptors. + * + * @param registry the registry + */ @Override public void addInterceptors(InterceptorRegistry registry) { + // 注册需要检查token的控制器接口 registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/information/**"); registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/manager/**"); registry.addInterceptor(initAuthInterceptor()).addPathPatterns("/task/**"); diff --git a/src/main/java/com/dispose/interceptor/TokenInterceptor.java b/src/main/java/com/dispose/interceptor/TokenInterceptor.java index 09c69394..a25082c3 100644 --- a/src/main/java/com/dispose/interceptor/TokenInterceptor.java +++ b/src/main/java/com/dispose/interceptor/TokenInterceptor.java @@ -6,25 +6,43 @@ import com.dispose.service.UserAccountService; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.web.servlet.HandlerInterceptor; +import reactor.util.annotation.NonNull; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Objects; +/** + * The type Token interceptor. + */ @Slf4j public class TokenInterceptor implements HandlerInterceptor { + /** + * The User account service. + */ @Resource private UserAccountService userAccountService; + /** + * Pre handle boolean. + * + * @param request the request + * @param response the response + * @param handler the handler + * @return the boolean + * @throws Exception the exception + */ @Override public boolean preHandle(HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + @NonNull HttpServletResponse response, + @NonNull Object handler) throws Exception { + // 提取header中的Authorization字段里面的token值 String token = Objects.requireNonNull(request.getHeader("Authorization").replaceFirst("Bearer ", "")); ErrorCode err = userAccountService.authTokenCheck(token); + // 判断token是否合法 if (err != ErrorCode.ERR_OK) { response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=UTF-8"); diff --git a/src/main/java/com/dispose/service/impl/UserAccountServiceImpl.java b/src/main/java/com/dispose/service/impl/UserAccountServiceImpl.java index 78110f7d..e4d1da1a 100644 --- a/src/main/java/com/dispose/service/impl/UserAccountServiceImpl.java +++ b/src/main/java/com/dispose/service/impl/UserAccountServiceImpl.java @@ -2,6 +2,7 @@ package com.dispose.service.impl; import com.dispose.common.ConstValue; import com.dispose.common.ErrorCode; +import com.dispose.common.GlobalVar; import com.dispose.manager.UserAccountCacheManager; import com.dispose.mapper.UserAccountMapper; import com.dispose.pojo.entity.UserAccount; @@ -40,7 +41,7 @@ public class UserAccountServiceImpl implements UserAccountService { @Override public ErrorCode authTokenCheck(String token) { - if (ConstValue.GlobalConfigure.IS_VERIFY_TOKEN) { + if (GlobalVar.IS_VERIFY_TOKEN) { return userAccountCacheManager.verifyToken(token); } diff --git a/src/test/java/com/dispose/Global/InitTestEnvironment.java b/src/test/java/com/dispose/Global/InitTestEnvironment.java index e749fdc6..2d04978b 100644 --- a/src/test/java/com/dispose/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/Global/InitTestEnvironment.java @@ -1,5 +1,6 @@ package com.dispose.Global; +import com.dispose.common.ConstValue; import com.dispose.common.ErrorCode; import com.dispose.common.GlobalVar; import com.dispose.pojo.po.MReturnType; @@ -52,6 +53,7 @@ public class InitTestEnvironment { public static void initVirtualDevice() { GlobalVar.USED_VIRTUAL_DISPOSE_MODE = true; GlobalVar.IS_CHECK_REQUEST_TIMEOUT = true; + GlobalVar.IS_VERIFY_TOKEN = true; log.warn("Current Used Virtual Dispose Device"); }