2017-05-04 143 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