if (remember) {
String timeout = Play.configuration.getProperty("remember.timeout");
response.setCookie("name", login_name, "", "/", Integer.parseInt(timeout) * 60,false);//30 * 24 * 60 * 60
response.setCookie("pwd", password, "", "/", Integer.parseInt(timeout) * 60,false);
response.setCookie("keys", "true", "", "/", Integer.parseInt(timeout) * 60,false);
}else{
response.removeCookie("name");
response.removeCookie("pwd");
}
服务器端 在执行removeCookie时
/**
* Removes the specified cookie with path /
* @param name cookiename
*/
public void removeCookie(String name) {
removeCookie(name, "/");
}
/**
* Removes the cookie
* @param name cookiename
* @param path cookiepath
*/
public void removeCookie(String name, String path) {
setCookie(name, "", null, path, 0, false);
}
不是删除了cookie,而是置空,若没有 还会新建一个value为空的cookie。所以在取值判断时,需要判断value为空值。
Http.Cookie currentUser = request.cookies.get("name");
if(currentUser!=null && currentUser.value!=null&& !currentUser.value.equals("")){
String name=currentUser.value;
session.put("user",name);
}else{
if(session.get("user")==null){
Secures.logout();
}
}
否则就会出现执行if中语句,将session 中user值置为空值的情况。
String timeout = Play.configuration.getProperty("remember.timeout");
response.setCookie("name", login_name, "", "/", Integer.parseInt(timeout) * 60,false);//30 * 24 * 60 * 60
response.setCookie("pwd", password, "", "/", Integer.parseInt(timeout) * 60,false);
response.setCookie("keys", "true", "", "/", Integer.parseInt(timeout) * 60,false);
}else{
response.removeCookie("name");
response.removeCookie("pwd");
}
服务器端 在执行removeCookie时
/**
* Removes the specified cookie with path /
* @param name cookiename
*/
public void removeCookie(String name) {
removeCookie(name, "/");
}
/**
* Removes the cookie
* @param name cookiename
* @param path cookiepath
*/
public void removeCookie(String name, String path) {
setCookie(name, "", null, path, 0, false);
}
不是删除了cookie,而是置空,若没有 还会新建一个value为空的cookie。所以在取值判断时,需要判断value为空值。
Http.Cookie currentUser = request.cookies.get("name");
if(currentUser!=null && currentUser.value!=null&& !currentUser.value.equals("")){
String name=currentUser.value;
session.put("user",name);
}else{
if(session.get("user")==null){
Secures.logout();
}
}
否则就会出现执行if中语句,将session 中user值置为空值的情况。