我正在使用OpenID.如何使用户即使关闭浏览器窗口后仍能长时间登录?
如何存储和访问用户的User对象?
基本上,我想我真的不明白会话如何在Java中运行.
我无法检索cookie maxage它总是返回-1
创建cookie:
Cookie securityCookie = new Cookie("sec", "somevalue");
securityCookie.setMaxAge(EXPIRATION_TIME);
Run Code Online (Sandbox Code Playgroud)
检索cookie:
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if ("sec".equals(cookie.getName())){
int age = cookie.getMaxAge();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我总是年龄= -1
当我检查firefox cookie到期时,我看到奇怪的日期.
谢谢
当我添加如下cookie时:
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", null);
Run Code Online (Sandbox Code Playgroud)
然后它运行良好,但cookie成为最大年龄为的会话cookie -1.
当我尝试如下:
Map<String, Object> properties = new HashMap<>();
properties.put("domain", "test");
properties.put("maxAge", 31536000);
properties.put("secure", false);
properties.put("path","/");
FacesContext.getCurrentInstance().getExternalContext().addResponseCookie("Test", "Test", properties);
Run Code Online (Sandbox Code Playgroud)
然后我在任何地方都看不到cookie.我不明白为什么.
我正在使用Tomcat 7.
是否可以检查 cookie 何时到期?我尝试了以下方法:
首先我设置了三个cookie:
<cfcookie name="test1" value="" expires="10" />
<cfcookie name="test2" value="" expires="never" />
<cfcookie name="test3" value="" expires="now" />
Run Code Online (Sandbox Code Playgroud)
然后在另一个页面上我检查 cookie 数据:
<cfset cookies = getPageContext().getRequest().getCookies()>
<Cfoutput>
<cfloop index="c" array="#cookies#">#c.getName()#:#c.getMaxAge()#<br>
</cfloop>
</Cfoutput>
Run Code Online (Sandbox Code Playgroud)
但是MaxAge返回-1所有 cookie 而不是实际到期日期。我怎样才能得到实际的到期日期?