2017-05-04 142 views
0

我用下面的安全配置我的春节,启动应用程序:春季安全:删除cookie中注销

@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来的日期早于当前日期?

回答

1

您不应该删除cookie。一旦会话在服务器上关闭,cookie仍然无法使用,如果该人员返回,它将被替换。让它正常过期(默认情况下,当浏览器关闭时)。

+0

在客户端使用'JSESSIONID'如何?客户端是否明确将其包含在每个请求的标题中? –

+1

Cookie会自动作为请求中的标题发送。 – ThrawnCA

0

.deleteCookies("auth_code", "JSESSIONID")中加上JSESSIONID

logout().logoutUrl("/logout").logoutSuccessUrl("/login").deleteCookies("auth_code", "JSESSIONID").invalidateHttpSession(true) 
+0

也许我不清楚我的问题。我不想取消设置JSESSIONID。我不关心它。我想删除'auth_code'的cookie。要从服务器端删除cookie,是否必须将其过期时间设置为过期时间并重新设置?或deleteCookies是否够好?标题上没有任何内容告诉浏览器删除cookie。 –

+1

是的,就够了。当您在“deleteCookies”中设置cookie名称时,Cookie将由响应头中的CookieClearingLogoutHandler将maxAge设置为0 http://grepcode.com/file/repo1.maven.org/maven2/org.springframework.security/弹簧安全的web/4.0.2.RELEASE /组织/ springframework的/安全/网络/认证/注销/ CookieClearingLogoutHandler.java – shazin