【发布时间】:2014-10-22 10:54:53
【问题描述】:
我有一个 ColdFusion 网站,它根据登录用户的属性以编程方式处理 onrequeststart() 和 Application.cfc 中的“禁止”/“未授权”请求。例如(仅供参考:SESSION.User 在onSessionStart() 中初始化:
<cffunction name="onRequestStart" returnType="Boolean" output="false">
<cfargument name="targetPage" type="string" required="true">
<cfparam name="REQUEST.MinSecurityLevel" default="0" />
<cfparam name="REQUEST.IsLoginRequired" default="false" />
<cfif REQUEST.IsLoginRequired AND NOT SESSION.User.isLoggedIn()>
<cfscript>
SESSION.LoginMessage =
"Your session has timed out. Please log in again.";
SESSION.LastPageVisited =
getPageContext().getRequest().getRequestURI();
if (Len(Trim(getPageContext().getRequest().getQueryString())))
SESSION.LastPageVisited =
SESSION.LastPageVisited
& "?"
& getPageContext().getRequest().getQueryString();
</cfscript>
<cflocation url="/user/login/" addtoken="false" />
<cfelseif SESSION.User.getSecurityLevel() LT REQUEST.MinSecurityLevel>
<cfheader statuscode="403" statustext="Forbidden" />
</cfif>
<cfreturn true />
</cffunction>
在 IIS(版本 7)中,我有一个错误页面设置,类型为“执行 URL”和我的自定义 403 页面路径。
我能够触发此操作,它会正确显示我的自定义 403 页面,但它会返回 HTTP 响应代码 200。
这不应该返回 403 吗?
【问题讨论】:
-
我对您的要求感到困惑。标题表明您在询问是否应针对特定情况返回 403 代码,但您的问题似乎指出了一个问题,即您试图发送回 403 代码但实际上返回了 200 代码。你能澄清一下吗?如果是后者,我会遇到与 ColdFusion 处理的 404 代码相同的问题。
-
您想在 cfheader 之后立即执行 cfabort 以停止所有未来的处理吗?我认为这可以保证您返回 403。
-
@RandyJohnson 添加 CFABORT 具有相同的结果。不过,谢谢。
-
@Miguel-F 我想我在问这两个问题 - 它是否应该总是返回 403(最佳实践),为什么我的应用程序不返回 403?我在 ColdFusion/IIS 中有一个针对 404 的解决方案: 1. 不要使用 onMissingTemplate()。 2. 将 IIS 设置为“始终检查文件是否存在”。 3. 将 404 的自定义错误页面设置为自定义 404 路径的执行 URL(我的是“/missing-template/index.cfm”,然后在该模板文件的顶部添加:
。这对我来说非常有用,可以捕获所有 404 - CF 等。 -
查看您的代码,REQUEST.MinSecurityLevel 似乎始终为 0。SESSION.User.getSecurityLevel() 会返回负数吗?
标签: iis coldfusion http-status-code-403 httpforbiddenhandler