我正在发送带有 url 的基本身份验证。当用户单击下载按钮时,我会从前端收到身份验证详细信息。如果身份验证成功,它会将数据发送回前端,并将该数据作为文件下载。我的问题是当我尝试下载一个文件时,api 需要很长时间才能完成其请求。任何建议,将不胜感激
@RequestMapping(value = "/checkIfProtectedOrPublic/", method = RequestMethod.POST)
public ResponseEntity<byte[]> checkIfProtectedOrPublic(@RequestBody CheckProtectedData checkProtectedData) throws Exception
{
// first the get download parameter
// TODO: Change this later to read this from partner or customer
PrmMain loginParameter = prmMainRepository.findAllByCode("PROTECTED_LOGIN").get(0);
if (loginParameter == null)
throw new IllegalArgumentException("Protected Login Not Configured");
// now try and download the file to a byte array using commons - this bypasses CORS requirements
HttpGet request = new HttpGet(checkProtectedData.getDownloadLink());
String login = String.valueOf(loginParameter.getData().get("email"));
String password = String.valueOf(loginParameter.getData().get("password"));
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(login, password));
try
(
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
CloseableHttpResponse response = httpClient.execute(request)
)
{
// if there was a failure send it
if (response.getStatusLine().getStatusCode() != HttpStatus.OK.value())
return new ResponseEntity<>(HttpStatus.valueOf(response.getStatusLine().getStatusCode()));
// send back the contents
HttpEntity entity = response.getEntity();
if (entity != null)
{
// return it as a String
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.parseMediaType(entity.getContentType().getValue()));
header.setContentLength(entity.getContentLength());
header.set("Content-Disposition", "attachment; filename=" + checkProtectedData.getFileName());
return new ResponseEntity<>(EntityUtils.toByteArray(entity), header, HttpStatus.OK);
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}