PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 5, 2022

[FIXED] How to expose Images securely so user can use it directly in <img src="">

 May 05, 2022     html, image, java, model-view-controller, rest     No comments   

Issue

We have images in our Database.

End user want to access it using

<img src="https://mydata.ra.com/Api/image">
  1. We informed end user to call our API and we will return base64 and use that. (Call from Backend code or javascript)
  2. We are planning to give API which returns URL to end user and that URL contains information which expire in 15 minutes and end user can access image from that URL

What other strategy can be used here to provide access to images which can directly used in IMG Src?


Solution

You can use a Servlet.

In the doGet() method, after checking for security (login status, oauth, anti-CRSF, whatever you usually check), transform the path into a key to access the image on the database:

e.g.: if the Servlet is mapped on /img/fetcher/* you can call it with

<img src='{yourcontext}/img/fetcher/{imguuid}">

Then it's just a matter to retrieve the imguuid from the URI, translate it into you database key, perform a select and serve it on your servlet response.

        byte [] image = yourBuLogic.getImage(request.getRequestURI());

        response.setBufferSize(DEFAULT_BUFFER_SIZE);
        response.setContentType("image/png"); // You can store this on the database as well
        response.setHeader("Content-Length", String.valueOf(image.length));
        response.getOutputStream().write(image);
        response.getOutputStream().flush();


Answered By - BigMike
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing