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);
}
}

View File

@@ -0,0 +1,18 @@
package com.honeycombis.honeycom.log.api.feign;
import com.honeycombis.honeycom.admin.api.dto.SysLogDTO;
import com.honeycombis.honeycom.common.core.constant.ServiceNameConstants;
import com.honeycombis.honeycom.common.core.util.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(contextId = "remoteSpdmLogService", value = ServiceNameConstants.SPDM_SERVICE)
public interface RemoteSpdmLogService {
@PostMapping("/honeycom-spdm/spdm-log/saveLog")
R saveLog(@RequestBody SysLogDTO sysLogDto);
}

View File

@@ -12,9 +12,11 @@ import com.honeycombis.honeycom.common.data.tenant.TenantBroker;
import com.honeycombis.honeycom.common.data.tenant.TenantContextHolder;
import com.honeycombis.honeycom.common.log.util.LogTypeEnum;
import com.honeycombis.honeycom.log.api.entity.SysLog;
import com.honeycombis.honeycom.log.api.feign.RemoteSpdmLogService;
import com.honeycombis.honeycom.log.api.vo.PreLogVO;
import com.honeycombis.honeycom.log.mapper.SysLogMapper;
import com.honeycombis.honeycom.log.service.SysLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -33,8 +35,11 @@ import java.util.stream.Collectors;
* @since 2017-11-20
*/
@Service
@RequiredArgsConstructor
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
private final RemoteSpdmLogService remoteSpdmLogService;
/**
* 批量插入前端错误日志
* @param preLogVoList 日志信息
@@ -86,6 +91,12 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
BeanUtils.copyProperties(sysLog, log, "createTime");
return baseMapper.insert(log);
});
// 退出登录记录到spdm日志中去
if ("退出登录".equals(sysLog.getTitle())) {
remoteSpdmLogService.saveLog(sysLog);
}
return Boolean.TRUE;
}

View File

@@ -19,7 +19,7 @@ public class SpdmService {
public void asyncSaveLog(SysLogDto sysLogDto) {
try {
SysLogDto sysLog = new SysLogDto();
sysLog.setTitle("登录成功");
sysLog.setTitle(sysLogDto.getTitle());
sysLog.setServiceId("simulation-system");
sysLog.setTenantId(sysLogDto.getTenantId());
sysLog.setCreateBy(sysLogDto.getCreateBy());