REM:
1. 更改token校验配置项可以动态配置
This commit is contained in:
HuangXin 2020-05-06 16:32:45 +08:00
parent 4a05ce23ee
commit 591c6f8561
6 changed files with 43 additions and 7 deletions

View File

@ -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.
*/

View File

@ -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;
}

View File

@ -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/**");

View File

@ -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");

View File

@ -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);
}

View File

@ -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");
}