删除 coldfusion 服务器上的所有会话



有没有办法删除冷融合服务器上特定应用程序的所有当前会话。我想强制所有用户更新他们的会话变量并添加新的会话变量。

我想到了类似的事情

<Cfset applicationStop()>

但我不确定它是否会删除所有会话。即便如此,如果是这样,我仍然需要阻止它删除所有应用程序的所有会话。我只想清除 1 个应用程序的所有会话,并强制为该网站/应用程序上的所有用户执行 OnSessionStart(在 application.cfc 中(。

下面是一个Application.cfc的代码片段,它允许您重置应用程序的所有会话变量。控制变量是 application.loaded。 您需要提供将更改此变量值的代码以强制重新加载会话。当你的代码将 application.load 设置为 now(( 时,它的日期/时间将比 session.loaded 新,它将重置用户会话。 此版本是用 CF2016 级别的 CFML 编写的。

此代码更像是一个模板,您必须为实现进行修改。

应用.cfc:

component displayname="myApp" {
this['Name'] = "myApp";
this['ApplicationTimeout'] = CreateTimeSpan(0, 12, 0, 0);
this['sessionTimeout'] = CreateTimeSpan(0, 0, 45, 0);
this['SessionManagement'] = true;
this['ClientManagement'] = false;
this['SetClientCookies'] = true;
public boolean function onApplicationStart() {
// app variable for session scope refresh
application['loaded'] = now();
return true;
} // onApplicationStart()
public void function onSessionStart() {
// this individual session loaded flag
session['loaded'] = now();
return;
} // onSessionStart()
public boolean function onRequestStart(required string targetPage) {
// if the applicaiton.loaded variable is more recent, force this session to be reset
if (application.keyExists("loaded") && session.keyExists("loaded") && application.loaded > session.loaded) {
// pick one or more of these FOUR options to reset the session.
// call the J2EE method of invalidating a session
getPageContext().getSession().invalidate();
// OR use the CF method
sessionInvalidate();
// OR clear the session struct
session.clear();
// OR clear important session variables that tell your app that the user is logged out, this will need to change based on YOUR implementation
session['user'] = "";
// if you clear the session with a form of invalidate(); onSessionStart() should be called to reset the session.loaded var.  It can also be set here.
session['loaded'] = now();
// redirect to the target page, which should send the user back to the login page because the session was reset
location(url=arguments.targetPage, addtoken=false);
}
return true;
} // onRequestStart()
} // component

当我为网站构建这种系统时,一个奇怪的是;虽然调用了applicationStop((,但会话并没有被清除。 您可能会认为会话会在应用程序停止时被销毁,但事实并非如此。 这就是我构建这种方法的原因。 会话似乎与单个站点 cookie 相关联,并且独立于它们可能所在的应用程序。

我不使用单一登录方法,请为每个应用程序使用单独的应用程序.cfm。

当您注销一个应用程序时,将仅结束一个应用程序会话。

我无法添加此评论,因为我没有权限。

我喜欢重置会话范围而不是删除清除它们,以便设置最新的变量。下面的代码演示如何刷新所有会话。如果要删除以下代码中的会话,可以使用循环中的行<cfset StructClear(appSessions[sessionKey])>,而忽略从 Application.cfc 调用的OnApplicationStart()方法。

setMaxInactiveInterval()方法是一个未记录的函数,它与session作用域相关联,您可以在其中设置会话超时。您可以通过 Java 对象coldfusion.runtime.SessionTracker遍历应用程序中的所有活动会话,然后将该方法应用于它们。之后,重置应用程序范围,以便再次应用默认会话超时。

<cfscript>
appName = "MY_APP_NAME";
jSessTracker = CreateObject("java", "coldfusion.runtime.SessionTracker");
appSessions = jSessTracker.getSessionCollection(JavaCast("string", appName));
</cfscript>
<cfloop collection="#appSessions#" item="sessionKey">
<cfset appSessions[sessionKey].setMaxInactiveInterval(JavaCast("long", 1))>
</cfloop>
<cfinvoke component="Application" method="OnApplicationStart">

最新更新