【发布时间】:2020-06-05 02:21:58
【问题描述】:
我正在尝试使我的代码创建的 cookie 无效。我想这样做创建一个新的端点来注销。我必须根据端点是否返回数据在两条路径之间进行选择。当我在本地运行代码时,它就像一个魅力。删除 cookie 并重定向到正确的 url,但是,如果我测试它部署在远程服务器上,它不起作用,这意味着它正确重定向但不会使 cookie 过期,也不会修改其值。我的整个代码必须是后端代码,因此我无法重定向到会删除 cookie 并再次重定向的 javascript。
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public void cookieKiller(
@CookieValue(name = "theCookie", required = false) Cookie myCookie,
HttpServletResponse response, HttpServletRequest request) {
if (myCookie != null) {
} else { // Just being cautious, if the cookie is not retrieved, would be overwritten anyway just in case
myCookie = new Cookie("theCookie","");
}
myCookie.setMaxAge(0);
myCookie.setValue("");
myCookie.setPath("/");
response.addCookie(myCookie);
boolean endpointHealthy = true;
if(haveITheEndpointUrls()) {
endpointHealthy = false;
} else {
try {
String eaiResponseJson = restTemplateAutowired.getForEntity(new URI(oneUrl),String.class).getBody();
if (eaiResponseJson != null && !eaiResponseJson.isBlank()) {
response.sendRedirect(eaiSamlUrl);
} else {
endpointHealthy = false;
}
} catch (IOException e) {
endpointHealthy = false;
} catch (ResourceAccessException e) {
endpointHealthy = false;
} catch (HttpClientErrorException e) {
endpointHealthy = false;
} catch (Exception e) {
endpointHealthy = false;
}
}
if (!endpointHealthy) {
try {
response.sendRedirect(request.getContextPath() + "/internal/Path");
} catch (IOException ex) {
// ...
}
}
}
如果我添加一个新的 cookie,生成的域与应用程序前面介绍的原始域不同。作为后面的“domain.com”和前面的“.domain.com”,所以我想这是一个域名
我一直在尝试更改路径、更改值等,但它也不起作用,我在 stackoverflow 上浏览了很多以寻找答案但没有任何结果。关于域的一些建议或线索或我可能遗漏的任何提示?
【问题讨论】:
标签: java spring session cookies session-cookies