mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
校验token
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@ package com.gitee.sop.gateway.service.validate;
|
||||
|
||||
import com.gitee.sop.gateway.common.ApiInfoDTO;
|
||||
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.exception.ApiException;
|
||||
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 lombok.extern.slf4j.Slf4j;
|
||||
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.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -308,8 +310,16 @@ public class ApiValidator implements Validator {
|
||||
*
|
||||
* @param apiRequestContext 参数
|
||||
*/
|
||||
protected void checkToken(ApiRequestContext apiRequestContext) {
|
||||
// todo:校验token
|
||||
protected void checkToken(ApiRequestContext apiRequestContext, ApiInfoDTO apiInfoDTO) {
|
||||
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
|
||||
|
Reference in New Issue
Block a user