background

Recently received a project, there is such a demand, need to download some files and preview permission control, only allow members to use, after looking for some information and finally decided to use Nginx with Java service to achieve this function.

Use this method only for some business files, and do not use this method for static files in web pages, so as not to increase the burden on the server

Configure Nginx

Use in configurationNginxA port is opened for8086File server, added in configurationinternalProperty indicating that the file is internally accessible only. Upon receiving the request, the proxy forwards the request to port as8080theJavaPermission authentication service.

This is basically all the configuration for Nginx. It’s not very complicated. Remember to restart the configuration to make sure it works.

Java service

The background to useSpringBootSet up the service and pass the Settings after being certifiedResponseHeadernoticeNginxWhether to return the requested file.

  • Content-Type
  • Content-Disposition
  • X-Accel-Redirect

Three main headers

More about the header

The complete code