How to manually log out a user with spring security?

后端 未结 9 1302
情话喂你
情话喂你 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:27

    Simply do like this (the ones commented by "concern you") :

        Authentication auth = SecurityContextHolder.getContext().getAuthentication(); // concern you
    
        User currUser = userService.getUserById(auth.getName()); // some of DAO or Service...
    
        SecurityContextLogoutHandler ctxLogOut = new SecurityContextLogoutHandler(); // concern you
    
        if( currUser == null ){
            ctxLogOut.logout(request, response, auth); // concern you
        }
    

提交回复
热议问题