package com.mindskip.xzs.configuration.spring.security; import com.mindskip.xzs.base.SystemCode; import com.mindskip.xzs.configuration.property.SystemConfig; import com.mindskip.xzs.domain.UserEventLog; import com.mindskip.xzs.event.UserEvent; import com.mindskip.xzs.service.UserService; import com.mindskip.xzs.domain.User; import lombok.AllArgsConstructor; import org.springframework.context.ApplicationEventPublisher; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler; import org.springframework.stereotype.Component; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Date; /** * @version 2.2.0 * @description: 用户登出 * Copyright (C), 2020-2021, 武汉思维跳跃科技有限公司 * @date 2021 /9/7 9:45 */ @Component @AllArgsConstructor public class RestLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler { private final ApplicationEventPublisher eventPublisher; private final UserService userService; private final SystemConfig systemConfig; @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { org.springframework.security.core.userdetails.User springUser = (org.springframework.security.core.userdetails.User) authentication.getPrincipal(); if (null != springUser) { User user = userService.getUserByUserName(springUser.getUsername()); UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date()); userEventLog.setContent(String.format("%s 登出了%s", user.getUserName(), systemConfig.getName())); eventPublisher.publishEvent(new UserEvent(userEventLog)); RestUtil.response(response, SystemCode.OK); } } }