HTTP_REFERER 和 USER_AGENT 很容易被欺骗。话虽如此,如果您想防止热链接,那么 HTTP_REFERER 是一个很好的开始,可以将其限制为来自您自己的应用程序的调用。
使用 Apache mode_security
SecFilterSelective "HTTP_REFERER" "^[^\?]*mydomain\.com"
将上述内容添加到带有字体的目录将拒绝来自其他站点的所有不合规请求。
为了提高安全性,当有人使用您的应用程序时,您在服务器上给他们一个会话(例如 PHP),并在那里存储一个 uniqueId。
<?PHP
// #header.php - in the head of the page that uses the font
// ...
if( !isset( $_SESSION['uniqueId'] ) ) {
$_SESSION['uniqueId'] = rand( pow(2,16), pow(2,31) );
}
$uniqueId = $_SESSION['uniqueId'];
echo '<script type="text/javascript" src="http://foo.com/getFont.php?u='.$uniqueId.'"></script>';
?>
这为字体服务。
<?PHP
// #getFont.php - serve your fonts from here
// ...
if( !isset( $_GET['u'] ) || !isset( $_SESSION['uniqueId'] ) || $_SESSION['uniqueId']!=$_GET['u'] ) {
die('Bad Request');
}
// cat out the file contents here for the request font file
?>
然后,您为您的字体引用一个动态页面(例如 getFont.php?uniqueId=foo),并且仅当 unqiueId 与他们的会话匹配时才返回字体文件,否则您认为它是一个欺骗性的引用热链接。这与将文件放在仅经过身份验证的用户的目录中基本相同,但只有在用户登录时才有效,而上述方法只是要求用户在加载字体之前加载页面,以防止热链接.