我希望cookie在用户会话过期时过期。它可以是纯javascript,XPages-SSJS。
发布于 2013-09-10 19:01:56
在每个页面加载中,将cookie设置为在当前时间+会话长度时过期。
例如,如果您的会话长达1小时,请在页面加载时添加以下内容:
var time = new Date().getTime(); // get the current time
time += 3600 * 1000; // add 1 hour to the current time
document.cookie = 'cookiedata=' + cookiedata + '; expires=' + time.toGMTString() + ';';在本例中,cookiedata是您在cookie中存储的内容。
发布于 2013-09-10 19:43:29
此XSnippet上的代码可以在注销按钮后面使用。这将清除8.5.x服务器的sessionScope变量,但不会清除9.0服务器的http://openntf.org/XSnippets.nsf/snippet.xsp?id=clear-session-whole-server变量:
如果想要清除R9上的sessionScope映射,则需要获取映射的句柄,迭代键并清除它们,如XSnippet:http://openntf.org/XSnippets.nsf/snippet.xsp?id=clear-session-current-nsf所示
发布于 2013-09-10 20:15:15
以下是从服务器端修改会话cookie过期日期的示例:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.beforeRenderResponse>
<![CDATA[#{javascript:var sessionId = facesContext.getExternalContext().getRequest().getSession().getId();
var response = facesContext.getExternalContext().getResponse();
var maxAge = facesContext.getApplication().getApplicationProperty("xsp.session.timeout", "30");
var maxAge = maxAge * 60;
response.addHeader("Set-Cookie","SessionID=" + sessionId + "; path=/; max-age=" + maxAge)}]]>
</xp:this.beforeRenderResponse>
</xp:view>但是请记住,设置cookie的过期时间将会有一些副作用,f.e.如果要关闭浏览器,则不会自动删除cookie。因为cookie对整个服务器都有效( path =/),所以这也会影响在同一路径上运行的其他应用程序。
https://stackoverflow.com/questions/18717033
复制相似问题