我正在使用 Spring Boot、Spring MVC 和 Spring Security。我添加了 JWT 授权,因此我需要使我的应用程序 session 无状态,因此我在安全配置中添加了相应的参数:
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
但是当我向我的应用程序发出任何请求时,我会得到 JSESSIONID 作为 cookie。我尝试通过将此代码添加到我的 jwt 过滤器来解决该问题:
Cookie[] cookies = httpServletRequest.getCookies();
if(cookies!=null)
for (int i = 0; i < cookies.length; i++) {
cookies[i].setMaxAge(0);
httpServletResponse.addCookie(cookies[i]);
}
但是没有帮助,那么最终如何删除它??
我的完整安全代码:
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests()
.antMatchers("/user/login").permitAll().antMatchers("/user/get/**").hasRole(Role.BOT.toString()).antMatchers("/", "/login**","/callback/", "/webjars/**", "/error**")
.permitAll().anyRequest().authenticated();
http.apply(new JwtFilterConfiguer(provider));
}
最佳答案
MaxAge 为 -1 表示您希望 cookie 在 session 期间持续存在。您想将 MaxAge 设置为 0。
摘自[API文档][1]:
A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted.
请您也可以点击此链接'https://www.baeldung.com/java-servlet-cookies-session '
关于java - 如何从 cookie 中删除 JSESSIONID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55294993/