How should I load images if I use token-based authentication

前端 未结 2 1435
栀梦
栀梦 2021-01-07 16:16

I have a client-side application on domain client-domain.com and a server-side application on domain server-domain.com. There is an API on the serv

2条回答
  •  孤独总比滥情好
    2021-01-07 16:52

    There are three methods to solve it, the best approach to solve it is using the signed URLs


    1. The first method simply creates a route without authentication (anonymous access) with a signature hash parameter that indicates if the resource can be loaded or not.
    
    

    When the server receives the request you must validate the guid if the expiration time not been reached and, of course, check if guid is a valid signature.

    This approach is used by several files/documents servers like Dropbox, S3, CDN providers, etc.

    See the technique in some companies.

    • https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html#private-content-overview-choosing-duration

    • https://client.cdn77.com/support/knowledgebase/cdn-resource/how-do-i-set-up-signed-urls


    1. The second method is passed the token by querystring with the image URL.

      • This method is not recommendable because expose clearly the url and many servers sometimes write and expose public logs of URL accessed. The bad notice is that the JWT exposed normally the user can get control a lot of features further the image load.
    
    

    When the server receives the request you must validate the token by querystring and response with the content.


    1. The third method creates an authenticated cookie to validate the access of the image.

      • This method is not recommendable because is out of API pattern (webapi/token based authentication in general).

    When the server receives the request you need to validate if the validate cookie is valid.

提交回复
热议问题