How to manually log out a user with spring security?

后端 未结 9 1235
情话喂你
情话喂你 2020-11-29 21:06

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

SecurityContextHolder.get         


        
相关标签:
9条回答
  • 2020-11-29 21:35

    To log out a user in a web application you can also redirect him to the logout page. The LogoutFilter is then doing all the work for you.

    The url of the logout page is set in the security configuration:

    <sec:http ...>
      ...
      <sec:logout logout-url="/logout" logout-success-url="/login?logout_successful=1" />
      ...
    </sec:http>
    
    0 讨论(0)
  • 2020-11-29 21:36

    new SecurityContextLogoutHandler().logout(request, null, null);

    0 讨论(0)
  • 2020-11-29 21:39

    Right Oledzki, I am using the following for example inside my controller to logout and redirect the user to the login page in spring security 4.2.3

    SecurityContextHolder.clearContext();
    
    if(session != null)
        session.invalidate();
    
    return "redirect:/login";
    
    0 讨论(0)
提交回复
热议问题