| | |
| | | package com.ard.work.component.forest; |
| | | |
| | | import com.ard.work.config.TyConfiguration; |
| | | import com.ard.work.domain.receiver.AccessTokenReceiver; |
| | | import com.ard.work.service.IArdServerService; |
| | | import com.dtflys.forest.http.ForestRequest; |
| | | import com.dtflys.forest.interceptor.Interceptor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | @Slf4j |
| | | public class TokenInterceptor<T> implements Interceptor<T> { |
| | | @Resource |
| | | private TyConfiguration tyConfiguration; |
| | | private IArdServerService ardServerService; |
| | | |
| | | /** |
| | | * 该方法在请求发送之前被调用, 若返回false则不会继续发送请求 |
| | |
| | | */ |
| | | @Override |
| | | public boolean beforeExecute(ForestRequest req) { |
| | | log.info("invoke Simple beforeExecute"); |
| | | // 执行在发送请求之前处理的代码 |
| | | // 从缓存里获取token |
| | | AccessTokenReceiver accessToken = tyConfiguration.getAccessToken(); |
| | | log.debug("invoke Simple beforeExecute"); |
| | | String baseUrl = (String) req.getVariableValue("baseUrl"); |
| | | AccessTokenReceiver accessToken = ardServerService.getAccessTokenByBaseUrl(baseUrl); |
| | | req.addHeader("x-cid", accessToken.getCompanyId()); |
| | | req.addHeader("x-token", accessToken.getAccessToken()); |
| | | // req.addHeader("Authorization", "")); // 添加Header |
| | | // req.addQuery("username", "foo"); // 添加URL的Query参数 |
| | | return true; // 继续执行请求返回true |
| | | } |
| | | } |