【发布时间】:2021-11-11 19:56:21
【问题描述】:
目前在 Spring Boot 项目中通过如下配置设置 http only cookie。
当我调用以下端点时,此 cookie 设置正确。
@Bean
public CookieSerializer defaultCookieSerializer() {
DefaultCookieSerializer cookie = new DefaultCookieSerializer();
cookie.setDomainNamePattern(".*");
cookie.setCookieName("my_cookie");
cookie.setUseSecureCookie(true);
cookie.setUseHttpOnlyCookie(true);
cookie.setCookieMaxAge(1200);
return cookie;
}
可以看到,名为my_cookie 的cookie 被设置了2 分钟。
在同一项目中的控制器中,我有以下控制器方法。
如果我输入错误块,我希望删除名为my_cookie 的cookie。我该怎么做?
这是我为此找到的最接近的问题,但考虑到我通过配置设置它,情况并不相同。
https://stackoverflow.com/questions/9821919/delete-cookie-from-a-servlet-response
@PostMapping(value = "/endpoint")
public List CustomResponse(
@RequestBody Request request,
) throws Exception {
CustomResponse response = null;
if (otherCookie != null) {
CustomResponse response = // perform some other rest request and get value from there
}
if (response == null) {
// I want to delete the cookie named `my_cookie` at this stage.
throw new CustomException('name');
}
return response;
}
【问题讨论】:
-
看看这是否有帮助:stackoverflow.com/questions/33118342/….
-
This Atta Shah 的文章提供了另一种设置、检索和删除 cookie 的方法。
标签: java spring spring-boot spring-mvc cookies