2

我有一个 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 吗?

4

1 回答 1

0

好像我在上面对@Miguel-F 的回复中找到了解决方案。我只是模仿了我为处理 404 错误所做的部分工作:

/缺失模板/index.cfm:

<cfheader statuscode="404" statustext="Not Found" />

/未授权/index.cfm:

<cfheader statuscode="403" statustext="Forbidden" />

我认为向 403 处理程序页面添加 403 标头会导致无限循环,但它工作正常。

我还添加了 onRequestStart() 因为一旦触发 403 就不需要进一步处理:

<cfelseif SESSION.User.getSecurityLevel() LT REQUEST.MinSecurityLevel>
    <cfheader statuscode="403" statustext="Forbidden" />

    <cfreturn false />
</cfif>
于 2014-08-28T15:15:42.370 回答