校验token

This commit is contained in:
六如
2025-02-27 20:08:27 +08:00
parent c2664c4e37
commit b0a68908bd
2 changed files with 45 additions and 2 deletions

View File

@@ -0,0 +1,33 @@
package com.gitee.sop.gateway.interceptor.internal;
import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.common.enums.YesOrNoEnum;
import com.gitee.sop.gateway.interceptor.RouteInterceptor;
import com.gitee.sop.gateway.request.ApiRequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 校验token
*
* @author 六如
*/
@Component
@Slf4j
public class TokenValidateInterceptor implements RouteInterceptor {
@Override
public void preRoute(ApiRequestContext context, ApiInfoDTO apiInfoDTO) {
Integer isNeedToken = apiInfoDTO.getIsNeedToken();
if (YesOrNoEnum.of(isNeedToken) == YesOrNoEnum.NO) {
return;
}
// 这里做校验token操作如从redis查询token是否存在
// 走到这里token肯定有值
String appAuthToken = context.getApiRequest().getAppAuthToken();
log.info("访问token={}", appAuthToken);
// token不对抛出下面这个异常即可
// throw new ApiException(ErrorEnum.AOP_INVALID_AUTH_TOKEN, context.getLocale());
}
}

View File

@@ -2,6 +2,7 @@ package com.gitee.sop.gateway.service.validate;
import com.gitee.sop.gateway.common.ApiInfoDTO; import com.gitee.sop.gateway.common.ApiInfoDTO;
import com.gitee.sop.gateway.common.enums.StatusEnum; import com.gitee.sop.gateway.common.enums.StatusEnum;
import com.gitee.sop.gateway.common.enums.YesOrNoEnum;
import com.gitee.sop.gateway.config.ApiConfig; import com.gitee.sop.gateway.config.ApiConfig;
import com.gitee.sop.gateway.exception.ApiException; import com.gitee.sop.gateway.exception.ApiException;
import com.gitee.sop.gateway.message.ErrorEnum; import com.gitee.sop.gateway.message.ErrorEnum;
@@ -18,6 +19,7 @@ import com.gitee.sop.gateway.service.manager.dto.IsvDTO;
import com.gitee.sop.support.enums.ApiModeEnum; import com.gitee.sop.support.enums.ApiModeEnum;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -308,8 +310,16 @@ public class ApiValidator implements Validator {
* *
* @param apiRequestContext 参数 * @param apiRequestContext 参数
*/ */
protected void checkToken(ApiRequestContext apiRequestContext) { protected void checkToken(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfoDTO) {
// todo:校验token Integer isNeedToken = apiInfoDTO.getIsNeedToken();
if (YesOrNoEnum.of(isNeedToken) == YesOrNoEnum.NO) {
return;
}
// 这里做校验token操作
String appAuthToken = apiRequestContext.getApiRequest().getAppAuthToken();
if (StringUtils.isBlank(appAuthToken)) {
throw new ApiException(ErrorEnum.AOP_INVALID_AUTH_TOKEN, apiRequestContext.getLocale());
}
} }
@PostConstruct @PostConstruct