zhaowenyu
2025-10-17 1ab85e9fd1b392d36c6f2b08754249e4574ea4e8
ard-work/src/main/java/com/ard/work/component/forest/TokenInterceptor.java
@@ -3,14 +3,14 @@
import com.ard.work.domain.receiver.AccessTokenReceiver;
import com.ard.work.service.IArdServerInfoService;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import com.dtflys.forest.interceptor.ForestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class TokenInterceptor<T> implements Interceptor<T> {
public class TokenInterceptor implements ForestInterceptor {
    @Resource
    private IArdServerInfoService ardServerService;
@@ -21,11 +21,19 @@
     */
    @Override
    public boolean beforeExecute(ForestRequest req) {
        log.debug("invoke Simple beforeExecute");
        try {
        String serverId = (String) req.getVariableValue("serverId");
        AccessTokenReceiver accessToken = ardServerService.getAccessToken(serverId);
            if (accessToken == null) {
                log.warn("AccessToken 为 null, serverId={}", serverId);
                return false; // 阻止请求继续
            }
        req.addHeader("x-cid", accessToken.getCompanyId());
        req.addHeader("x-token", accessToken.getAccessToken());
        return true;  // 继续执行请求返回true
        } catch (Exception e) {
            log.error("TokenInterceptor 处理异常:{}", e.getMessage());
            return false;
        }
        return true;
    }
}