如何防止独立 Java Web 应用程序服务器上的热链接?
如何防止 Tomcat 等独立 Java Web 应用程序服务器上的热链接?
How do you prevent hotlinking on a standalone Java webapp server like Tomcat?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 Tuckey 的 URLRewriteFilter (正如其他人已经间接提到的那样)。来自文档:
Use Tuckey's URLRewriteFilter (as mentioned by others already indirectly). From the documentation:
正如布鲁诺所说,您可以检查合适的推荐人。
每个 HTTP 请求都包含一个引用标头,其中包含链接到当前请求的 URL 的 URL(或者,对于图像,链接到引用该图像的页面)。对于您的情况,它应该包含一个适当的引用 URL,该 URL 应该属于您自己的站点。
为了检测不允许的引用者,我认为您可以使用像 http://www.tuckey.org/urlrewrite 这样的过滤器/ 。您可以配置一个简单的规则来匹配不是来自您自己站点的每个图像请求,并禁止访问或将该 URL 重写为自定义“不允许热链接”图像。
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.
我不确定它是否已经存在,但是您可以轻松编写 Filter 检查是否存在与适当模式匹配的
Referer
标头(如您发布的链接中所述)。编辑:
您链接到的文章描述的规则基于
Referer
HTTP 标头(由浏览器发送以指示从哪个页面获取链接)。如果
Referer
标头与http 不匹配,则 Apache Httpd 上的
模式,然后重定向到.htaccess
中的以下规则与mod_rewrite
或多或少意味着什么://(www\\.)?yoursite\\.com/images/hotlink.jpeg
。过滤器是 web 应用程序中的一种标准机制,用于在请求发送到 servlet 进行处理之前拦截请求(如果需要,它们可以选择不重定向到 servlet)。
您将覆盖过滤器中的
doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
,测试request.getHeader("Referer")
是否匹配正确的模式(如果是) ,调用 chain.doFilter(request, response),否则发送重定向响应到其他某个图像(即“热链接”或其他内容),可能带有 403 状态代码。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.下面是一个过滤器实现示例:
它使用正则表达式模式的映射。如果请求与左侧的模式匹配并且存在引用者,则我们检查引用者是否与右侧的模式匹配。您可以在 web.xml 中进行配置:
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: