我的Application.cfc我正在尝试使用onRequestStart()
函数来保护我的页面:
<cffunction name="onRequestStart" access="public" returntype="boolean">
<cfargument type="String" name="TargetPage" required="true"/>
<!--- Define which pages don't need protection --->
<cfset APPLICATION.AllowedPages = "/index.cfm, /register.cfm">
<!--- Create an instance of the page-protect.cfc --->
<cfset APPLICATION.PageProtect = CreateObject("component", "page-protect")>
<!--- check if the current page is an allowed page --->
<cfif #ListFindNoCase(APPLICATION.AllowedPages, ARGUMENTS.TargetPage)# EQ 0>
<!--- if its not an allowed page, then protect it --->
<cfscript>
APPLICATION.PageProtect.PageProtectBasic(argumentcollection = session);
</cfscript>
</cfif>
<cfreturn true>
</cffunction>
此代码有效(稍后会看到)。 Page-protect.cfc非常简单,并且这样做:
<cfcomponent displayname="page-protect" output="false">
<cffunction name="PageProtectBasic" output="no">
<cfif NOT structKeyExists (SESSION, 'Auth')>
<cflocation url="/index.cfm" addtoken="no">
</cfif>
</cffunction>
</cfcomponent>
因此,如果Auth
范围内的SESSION
结构不存在,则此用户未登录,应该收回主页。另一个文件中的注销方法会从SESSION中删除Auth结构,并清除SESSION范围(如果我确实已经测试了它)。
onRequestStart()
页面保护最初起作用,但我注意到当我按下浏览器上的后退按钮时,它会显示我刚刚退出的上一页。这应该是一个受保护的页面,而不是显示,但我想它是一个浏览器缓存所以不是问题。但问题是,如果我点击此页面中的链接,它应该不允许它并将用户发送回主页(因为SESSION.Auth结构不存在且SESSION已被清除)。但它不再将用户发送回主页,它只显示一个ColdFusion错误页面,指出“SUTHION中未定义元素AUTH。{element_name}”。
因此,出于某种原因,尽管用户没有登录,它仍然没有回到主页,而是试图加载受保护的页面,然后因为SESSION.AUTH结构中的变量不存在而摔倒。我根本不明白我做错了什么。请帮忙!
答案 0 :(得分:3)
抱歉,这不适合评论框
我认为你要做的是这样的事情:
onApplicationStart() {
APPLICATION.PageProtect = CreateObject("component", "page-protect")>
}
onSessionStart() {
session.auth = false; // it is easier to work with if it always exists
}
onRequestStart() {
if (ListFindNoCase(APPLICATION.AllowedPages, ARGUMENTS.TargetPage) EQ 0)
// if its not an allowed page, then protect it
APPLICATION.PageProtect.PageProtectBasic(argumentcollection = session);
}
}
答案 1 :(得分:3)
虽然James Mohler提供了一些关于如何改进代码的非常有用的指示,但您遇到的问题与此无关。
用户可以在回击时看到这些页面的原因是因为它们在浏览器中缓存。这是浏览器试图提供帮助而不是从它已经看过的服务器请求数据。浏览器是一个优秀的互联网公民将会做它所告诉的事情。因此,您需要返回正确的HTTP标头,告诉它您不希望它缓存它们。 E.g。
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
所以在CF中这样做
<cfheader name="Cache-Control" value="no-cache, no-store, max-age=0, must-revalidate">
<cfheader name="Pragma" value="no-cache">
如果您将上述内容添加到与您有关的页面中,问题就会消失。
西蒙
答案 2 :(得分:1)
可能与您的问题无关,但这一行可能有几个问题。
<cfset APPLICATION.AllowedPages = "/index.cfm, /register.cfm">
问题编号1是主要斜线。除非arguments.TargetPage有这些斜杠,否则它们可能会得到预期的行为。
问题编号2是两个列表项之间的空格。前导空格是列表项的一部分,可能会导致register.cfm出现意外行为。