How do you prevent hotlinking on a standalone Java webapp server like Tomcat?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
You can check for an appropriate referer as Bruno said.
Every HTTP request contains a referer header that contains the URL that linked to the current URL being requested (or, for images, the page that referenced the image). In your case, it should contain an appropriate referer URL, which should belong to your own site.
In order to detect disallowed referers, I think you could use a filter like http://www.tuckey.org/urlrewrite/ . You can configure a simple rule that matches every image request not coming from your own site, and forbid the access or rewrite that URL to a custom 'Hotlinking not allowed' image.
Here's an example filter implementation:
It uses a map of Regex patterns. If a request matches the pattern on the left side and a referrer is present, then we check if the referrer matches the pattern on the right side. You can configure this in the web.xml:
Use Tuckey's URLRewriteFilter (as mentioned by others already indirectly). From the documentation:
I'm not sure whether it already exists, but you could easily write a Filter that checks whether there's a
Referer
header that matches the appropriate pattern (as described in the link you've posted).EDIT: What the article you've linked to describes is a rule based on the
Referer
HTTP header (which is sent by browsers to indicate from which page the link was obtained). The following rules in.htaccess
on Apache Httpd withmod_rewrite
more or less mean, if theReferer
header doesn't match thehttp://(www\\.)?yoursite\\.com
pattern, then redirect to/images/hotlink.jpeg
.Filters are a standard mechanism in webapps for intercepting requests before they're sent to the servlet for processing (and they can chose not to redirect to the servlet if needed).
You would override the
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
in your Filter, test whetherrequest.getHeader("Referer")
matches the right pattern, if so, callchain.doFilter(request, response)
, otherwise send a redirection response to some other image (that would say "hotlink" or whatever), possibly with a 403 status code.