我正在对第 3 方 api 进行休息调用以获取密钥,使用该密钥我正在调用其他 api 以获取数据,我在属性文件中有我的请求参数,如用户名、密码和 Urls 等,它们是常量,我将这些值设置在休息模板并尝试在整个应用程序中使用相同的模板。部署应用程序后,第一次调用密钥 api 给我密钥,使用该密钥我正在获取数据,但从第二次调用开始,密钥 api 抛出:400 错误请求: [{“错误”:“无效客户端”}]
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
lOGGER.info("factory in request " + factory);
InetSocketAddress address = new InetSocketAddress(SourceConstants.SOCKET_ADDRESS,
SourceConstants.PORT);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
factory.setProxy(proxy);
factory.setReadTimeout(SourceConstants.READ_TIMEOUT);
factory.setConnectTimeout(SourceConstants.CONNECT_TIMEOUT);
restTemplate.setRequestFactory(factory);
return restTemplate;
}
我的关键 API 调用类是
@Component
公共类 KeyCallUtil {
@Autowired
private CacheManager cacheManager;
@Autowired
private RestTemplate restTemplate;
@Cacheable(value = "getKey", key = "#root.methodName")
public String getKey(AuthorizationConfig DTO) throws Exception {
String endPoint = authDTO.getTokenUrl();
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("username", DTO.getUsername());
map.add("password", DTO.getPassword());
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, header);
ResponseDTO authResponseDTO = new ResponseDTO();
try {
ResponseDTO = restTemplate.postForObject(endPoint, request,ResponseDTO.class);
} catch (Exception exc) {
throw new ExternalException(exc.getMessage());
}
return ResponseDTO.getKey();
}
public void evictTokenCacheValue(String cacheKey) {
cacheManager.getCache(cacheKey).clear();
}
}
类调用数据api
@Component
公共类 RestCallUtil {
@Autowired 私钥CallUtil tokenCallUtil;
@Autowired
private RestTemplate restTemplate;
@SuppressWarnings("unchecked")
public InsightResponseDTO getData(AuthorizationConfig authDTO, SearchRequestDto requestDto,
i) throws ExternalException {
String endPoint = authDTO.getCubeUrl();
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
String key= tokenCallUtil.getKey(authDTO);
header.add("Authorization", key);
header.set("x-page-current", requestDto.getPage());
HttpEntity<SearchRequestDto> request = new HttpEntity<>(requestDto, header);
List<ResponseDTO> response = null;
List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
messageConverters.add(converter);
restTemplate.setMessageConverters(messageConverters);
ResponseDTO Response = new ResponseDTO();
ResponseEntity<Object> cubeResponse = null;
try {
cubeResponse = restTemplate.postForEntity(endPoint, request, Object.class);
Response = (List<ResponseDTO>) cubeResponse.getBody();
HttpHeaders headers = cubeResponse.getHeaders();
Response.setData(response);
Response.setHeaders(headers);
} catch (Exception e) {
throw new ExternalException(e.getMessage());
}
}
return Response;
}
}
两个 Api 都使用相同的代理、密码、用户名