Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manually log out a user with spring security?

Probably the answer is simple: How can I manually logout the currently logged in user in spring security? Is it sufficient to call:

SecurityContextHolder.getContext().getAuthentication().setAuthenticated(false);  

?

like image 440
Erik Avatar asked Apr 20 '11 08:04

Erik


People also ask

How do I logout of spring boot security?

Basic Configuration The basic configuration of Spring Logout functionality using the logout() method is simple enough: @Configuration @EnableWebSecurity public class SecSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(final HttpSecurity http) throws Exception { http //... .

What is the default logout URL defined by Spring Security?

According to Spring Security 4.0.0 document: 4.2.4 Logout Handling. The logout element adds support for logging out by navigating to a particular URL. The default logout URL is /logout, but you can set it to something else using the logout-url attribute.

What is the purpose of the Spring Security login logout module?

Spring Security provides login and logout features that we can use in our application. It is helpful to create secure Spring application.


1 Answers

It's hard for me to say for sure if your code is enough. However standard Spring-security's implementation of logging out is different. If you took a look at SecurityContextLogoutHandler you would see they do:

    SecurityContextHolder.clearContext(); 

Moreover they optionally invalidate the HttpSession:

    if (invalidateHttpSession) {         HttpSession session = request.getSession(false);         if (session != null) {             session.invalidate();         }     } 

You may find more information in some other question about logging out in Spring Security and by looking at the source code of org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler.

like image 124
Grzegorz Oledzki Avatar answered Oct 08 '22 14:10

Grzegorz Oledzki