java - 如何从 cookie 中删除 JSESSIONID?

标签 java spring spring-security

我正在使用 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/

相关文章:

java - 使用 insertLogical 时停止进入循环

java - 扩展可序列化的地理点 - 无法读回对象

java - 从两种不同的形式获取 servlet 中的值

java - 如何使用 java 读取从存储过程返回的类型数组?

java - 就线程或不同请求而言,Spring 的 SecurityContext 行为是什么?

grails - 使用jaxrs插件Spring Security设置在Grails中开发Webapi

java - 如何在 gradle 中为 JavaDoc 设置编码?

spring - WebSphere 7 中的 Spring 依赖注入(inject)(JSR 330 注释)不起作用

java - 将线程分组并为每个组设置不同的最大线程数

java - 使用spring security成功登录后如何将对象添加到 View 中?