1

因此,我们的源代码库中有一些受 htdigest 保护的文件夹。

我们想通过我们自己的自定义 php 处理程序记录无效登录。

我希望通过 apache ErrorDocument 指令来做到这一点。

所以我想我的问题是两层的。

a) 将 apache ErrorDocument 指令指向 php 脚本是否有效,并将其解析为 php 脚本(假设 php 已启动并为所述 httpd 运行)。

b) 我快速浏览了 http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 上的 http 状态代码列表


10.4.2 401 未经授权

该请求需要用户身份验证。响应必须包含一个 WWW-Authenticate 头字段(第 14.47 节),其中包含适用于所请求资源的质询。客户端可以使用合适的授权头域重复请求(第 14.8 节)。如果请求已包含授权凭证,则 401 响应表示已拒绝对这些凭证的授权。如果 401 响应包含与先前响应相同的质询,并且用户代理已经尝试了至少一次身份验证,则应该向用户呈现响应中给出的实体,因为该实体可能包含相关的诊断信息。HTTP 访问身份验证在“HTTP 身份验证:基本和摘要式访问身份验证”[43] 中进行了说明。


这听起来像是来自基于浏览器的用户代理的流程如下:

a) user hits protected url
b) apache responds with a 401 status code
c) user is presented by useragent with username + password prompt

我们只想记录无效的登录,而不是不知道(还)该页面需要身份验证的点击。

基本上,我会在这里做一些复制粘贴,但我们想要的是一个自定义的、基于 php 的替换:

Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

我们不想做的事情:

a) 被告知在这种情况下我们应该使用完整的基于 php 的登录系统。这已经完成,身份验证是 2 层的。用户必须通过基于 Web 的登录(基于会话)登录服务。第二层认证是基于http的认证。

b) 被告知设置一个完全定制的基于 php 的 http 身份验证处理程序。是的,我知道这是可能的,而且很容易,但我希望将实际的 htdigest 处理留在 apache 手中。但是,如果将 http auth 处理交给 apache 处理是不可能的,那么这最终将成为我们必须要做的事情。


总结一下:是否可以让 apache 在无效的 htdigest 登录(但不缺少)上解析基于 php 的脚本,以便我们可以在该脚本(日志、IP 块等)中执行操作?

4

1 回答 1

1

据我们所知,以下方法有效。即使基于过期的 htdigest 身份验证:

a) 将 ErrorDocument 401 指向基于 php 的脚本。

b) 我们的 php 401 脚本如下所示:

<?

if(isset($_SERVER['REDIRECT_REMOTE_USER'])){
    logError('authentication','invalid login attempt to '.(isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:'unknown').' using username '.$_SERVER['REDIRECT_REMOTE_USER']);
}

header($_SERVER['SERVER_PROTOCOL'].' 401 Authorization Required',true);
header('Status: 401 Authorization Required',true);

?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>401 Authorization Required</title> 
</head><body> 
<h1>Authorization Required</h1> 
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p> 
</body></html>

logError 函数是我们的自定义函数,但您可以将其替换为您喜欢的任何内容/在其中添加一些进一步的操作。

享受!

于 2011-09-04T08:11:24.863 回答