Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file that needs authentication token

I am implementing an AngularJS app with a REST backend (using Spring Boot).

I can currently download a file like this:

<td><a href="/api/datasheets/{{datasheet.id}}/documents/{{document.id}}/download" download>Download</a></td>

Now, I am adding security (using Spring Security) to my application and this now no longer works. The authentication of the AJAX calls works by adding x-auth-token in the HTTP header for each request.

But a simple href does not have the x-auth-token in the header ofcourse. I tried using $http.get() on an ng-click, but that cannot not work.

Is there a simple alternative?

like image 990
Wim Deblauwe Avatar asked Nov 10 '22 08:11

Wim Deblauwe


1 Answers

I had similar problem while implementing file downloads in angular. In my case, I was not able to handle blob in safari. What I did was: create a handler which returns a download token valid for say 5 second. Only authenticated user can get this token. Once you have the token, call a different handler which returns the file after validating the token and this handler is publicly accessible. So you don't need to send authentication header while downloading file.

I used itsdangerous library to implement timstamped token.

like image 168
Prakash Pandey Avatar answered Nov 15 '22 11:11

Prakash Pandey