Java Spring安全性:删除注销cookie

Java Spring安全性:删除注销cookie,java,spring,Java,Spring,我对我的Spring boot应用程序使用以下安全配置: @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .antMatchers("/login").permitAll() .and() .authorizeRequ

我对我的Spring boot应用程序使用以下安全配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()
        .authorizeRequests()
            .antMatchers("/login").permitAll()
        .and()
        .authorizeRequests()
            .antMatchers("/signup").permitAll()
        .and()
        .authorizeRequests()
            .anyRequest().authenticated()
        .and()
            .logout().logoutUrl("/logout").logoutSuccessUrl("/login").deleteCookies("auth_code").invalidateHttpSession(true)
        .and()
        // We filter the api/signup requests
        .addFilterBefore(
            new JWTSignupFilter("/signup", authenticationManager(),
                    accountRepository, passwordEncoder),
            UsernamePasswordAuthenticationFilter.class)
        // We filter the api/login requests
        .addFilterBefore(
            new JWTLoginFilter("/login", authenticationManager()),
            UsernamePasswordAuthenticationFilter.class)
        // And filter other requests to check the presence of JWT in
        // header
        .addFilterBefore(new JWTAuthenticationFilter(userDetailsServiceBean()),
            UsernamePasswordAuthenticationFilter.class);
} 
当我注销时,我想删除在登录期间设置的cookie。我使用
deleteCookie
,但是在标题中没有删除登录期间设置的cookie的概念。为什么?

我应该如何告诉浏览器删除cookie

现在,响应的标题包含:

Set-Cookie →JSESSIONID=E4060381B435217F7D68EAAE82903BB0;path=/;Secure;HttpOnly

我是否应该将cookie的过期时间设置为超过当前日期的日期?

您不需要删除cookie。一旦服务器上的会话关闭,cookie无论如何都无法使用,如果用户返回,cookie将被替换。只要让它正常过期(默认情况下,当浏览器关闭时)。

您不需要删除cookie。一旦服务器上的会话关闭,cookie无论如何都无法使用,如果用户返回,cookie将被替换。只要让它正常过期(默认情况下,当浏览器关闭时)。

中添加
JSESSIONID
。删除cookies(“auth_code”,“JSESSIONID”)


.deleteCookies(“auth\u code”、“JSESSIONID”)
中添加
JSESSIONID


有不同的方法可以使用spring security删除Cookie

  • 您可以在spring安全配置方法中添加以下注销方法

    .logout()

  • delete cookies方法在cookies以逗号(,)分隔时删除列表

    清除所有身份验证

    `.clearAuthentication(true)`
    
    然后使会话对象无效

    `.invalidateHttpSession(true)`
    
  • 显式终止并删除cookie,并将其映射到
    /logout
    路径

    @RequestMapping(path=“/logout”,method=RequestMethod.GET)

  • private void removeCookies(HttpServletRequest请求,HttpServletResponse响应){


    参考资料:

    使用spring security删除Cookie有不同的方法

  • 您可以在spring安全配置方法中添加以下注销方法

    .logout()

  • delete cookies方法在cookies以逗号(,)分隔时删除列表

    清除所有身份验证

    `.clearAuthentication(true)`
    
    然后使会话对象无效

    `.invalidateHttpSession(true)`
    
  • 显式终止并删除cookie,并将其映射到
    /logout
    路径

    @RequestMapping(path=“/logout”,method=RequestMethod.GET)

  • private void removeCookies(HttpServletRequest请求,HttpServletResponse响应){


    参考资料:

    可能我的问题不清楚。我不想取消设置JSSessionID。我不关心它。我想删除
    auth_code
    的cookie。要从服务器端删除cookie,我是否必须将其过期时间设置为过去某个时间,然后再次设置?或者删除cookie就足够了?服务器上没有任何设置告诉浏览器删除cookie的标题。是的,这就足够了。当您在
    deleteCookies
    中设置cookie名称时,CookieClearningLogoutHandler将在我的应用程序的响应标题中设置cookie的最大值为0,我有
    .logout().deleteCookies(“JSessID”,“JWT”).logoutSuccessUrl(/index.html”).clearAuthentication(正确)。无效HttpSession(正确)
    重定向到successUrl正在进行,但cookie未被删除…原因是什么?您找到此问题的修复程序吗?我面临同样的问题,无法删除服务器端的cookie。注销url后第一次重定向到登录页,要求用户密码。下次它没有重定向到登录页,未使用就加载主页r密码。可能我的问题不清楚。我不想取消设置JSSessionID。我不关心它。我想删除
    auth_code
    的cookie。要从服务器端删除cookie,我必须将其过期时间设置为过去某个时间并重新设置吗?或者删除cookie就足够了?头上没有任何东西告诉浏览器删除cookie的er。是的,这就足够了。当您在
    deleteCookies
    中设置cookie名称时,CookieClearningLogoutHandler会在我的应用程序的响应头中将cookie的最大值设置为0,我有
    .logout().deleteCookies(“JSSessionId”,“JWT”).logoutSuccessUrl(/index.html”).clearAuthentication(true).invalidateHttpSession(真)
    重定向到successUrl正在进行,但Cookie没有被删除…原因可能是什么?您找到解决此问题的方法了吗?我面临着同样的问题,无法在服务器端删除Cookie。注销后的第一次url重定向到登录页,询问用户密码。下次它不会重定向到登录页并在未使用的情况下加载主页r密码。
    JSESSIONID
    如何在客户端使用?客户端是否应明确将其包含在每个请求的标头中?Cookie将自动作为请求的标头发送。客户端如何使用
    JSESSIONID
    ?客户端是否应明确将其包含在每个请求的标头中?Cookie是autOMATALY在请求中作为标题发送。仅供参考您可以查看
    清除站点数据
    标题,您可能会发现此答案很有用。仅供参考您可以查看
    清除站点数据
    标题,您可能会发现此答案很有用
     Cookie rememberMeCookie = new Cookie("remember-me", "");
    
     rememberMeCookie.setMaxAge(0);
    
     response.addCookie(rememberMeCookie);
    
     securityService.logoutCurrentUser();
        }