fix:退出登录日志记录

This commit is contained in:
2026-04-14 16:55:04 +08:00
parent 1bb95ca0f8
commit a7be490dda
6 changed files with 54 additions and 5 deletions

View File

@@ -94,7 +94,7 @@ public class HoneycomCustomerController {
*/
@Inner(value = false)
@PostMapping("/v1/logout")
@SysLog("退出登录")
// @SysLog("退出登录")
public R<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) {
if (StrUtil.isBlank(authHeader)) {
return R.ok();

View File

@@ -5,10 +5,12 @@ import cn.hutool.core.util.StrUtil;
import com.honeycombis.honeycom.common.core.constant.CacheConstants;
import com.honeycombis.honeycom.common.core.util.SpringContextHolder;
import com.honeycombis.honeycom.common.security.dto.ObtainTokenDTO;
import com.honeycombis.honeycom.common.security.service.HoneycomUser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.CacheManager;
import org.springframework.security.authentication.event.LogoutSuccessEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.core.OAuth2AccessToken;
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
@@ -17,6 +19,10 @@ import org.springframework.security.oauth2.server.authorization.OAuth2TokenType;
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
import org.springframework.stereotype.Service;
import java.security.Principal;
import java.util.Map;
import java.util.Objects;
/**
* @author sunan
*/
@@ -77,8 +83,15 @@ public class HoneycomCustomerService {
// 清空access token
authorizationService.remove(authorization);
// 处理自定义退出事件,保存相关日志
Map<String, Object> attributes = authorization.getAttributes();
Authentication authentication = (Authentication) attributes.get(Principal.class.getName());
if (Objects.isNull(authentication)) {
return;
}
HoneycomUser honeycomUser = (HoneycomUser) authentication.getPrincipal();
SpringContextHolder.publishEvent(new LogoutSuccessEvent(new PreAuthenticatedAuthenticationToken(
authorization.getPrincipalName(), authorization.getRegisteredClientId())));
honeycomUser.getId(), authorization.getRegisteredClientId())));
}

View File

@@ -17,8 +17,11 @@
package com.honeycombis.honeycom.auth.support.handler;
import com.honeycombis.honeycom.admin.api.dto.SysLogDTO;
import com.honeycombis.honeycom.common.core.constant.SecurityConstants;
import com.honeycombis.honeycom.common.core.util.WebUtils;
import com.honeycombis.honeycom.common.log.util.SysLogUtils;
import com.honeycombis.honeycom.log.api.feign.RemoteHoneyLogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.http.HttpHeaders;
@@ -37,6 +40,9 @@ import org.springframework.stereotype.Component;
@Component
public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<LogoutSuccessEvent> {
@Resource
private RemoteHoneyLogService remoteLogService;
@Override
public void onApplicationEvent(LogoutSuccessEvent event) {
Authentication authentication = (Authentication) event.getSource();
@@ -54,7 +60,7 @@ public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<Lo
public void handle(Authentication authentication) {
log.info("用户:{} 退出成功", authentication.getPrincipal());
SysLogDTO logVo = SysLogUtils.getSysLog();
logVo.setTitle("退出成功");
logVo.setTitle("退出登录");
// 发送异步日志事件
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
@@ -67,7 +73,8 @@ public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<Lo
if (authentication instanceof PreAuthenticatedAuthenticationToken) {
logVo.setServiceId(authentication.getCredentials().toString());
}
logVo.setCreateBy(authentication.getName());
logVo.setCreateBy(String.valueOf(authentication.getPrincipal()));
remoteLogService.saveLog(logVo, SecurityConstants.FROM_IN);
}
}