如何在Spring Boot中注销特定用户

时间:2018-09-27 21:14:40

标签: spring-boot logout

我已经写了一个登录方法。现在,我希望用户从当前会话中注销。 但是,只能销毁所有会话。

这是我目前所拥有的:

我的登录方法:

@GetMapping("/")
public String root(Model model) {

    //show all current logged in users
    List<Object> loggedUsers = sessionRegistry.getAllPrincipals();
    for (Object principal : loggedUsers) {
        final User loggedUser = (User) principal;
        model.addAttribute("loggedInUser", loggedUsers); 
    }
    return "index";
}

注销类

@Component 公共类CustomLogoutSuccessHandler实现LogoutSuccessHandler {

@Override
public void onLogoutSuccess(HttpServletRequest request,
        HttpServletResponse response, Authentication authentication)
        throws IOException, ServletException {
    logger.info("Logout successfully with principal: " + authentication.getName());

    HttpSession hs = request.getSession();
    String username = request.getUserPrincipal().getName();
    String id = hs.getId();
    Enumeration e = hs.getAttributeNames();
    while (e.hasMoreElements()) {
        String attr = (String) e.nextElement();
        hs.setAttribute(attr, null);
    }
    hs.invalidate();
}

}

也许有人可以给我提示吗?

0 个答案:

没有答案