fix:退出登录日志记录
This commit is contained in:
@@ -94,7 +94,7 @@ public class HoneycomCustomerController {
|
|||||||
*/
|
*/
|
||||||
@Inner(value = false)
|
@Inner(value = false)
|
||||||
@PostMapping("/v1/logout")
|
@PostMapping("/v1/logout")
|
||||||
@SysLog("退出登录")
|
// @SysLog("退出登录")
|
||||||
public R<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) {
|
public R<Boolean> logout(@RequestHeader(value = HttpHeaders.AUTHORIZATION, required = false) String authHeader) {
|
||||||
if (StrUtil.isBlank(authHeader)) {
|
if (StrUtil.isBlank(authHeader)) {
|
||||||
return R.ok();
|
return R.ok();
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.honeycombis.honeycom.common.core.constant.CacheConstants;
|
import com.honeycombis.honeycom.common.core.constant.CacheConstants;
|
||||||
import com.honeycombis.honeycom.common.core.util.SpringContextHolder;
|
import com.honeycombis.honeycom.common.core.util.SpringContextHolder;
|
||||||
import com.honeycombis.honeycom.common.security.dto.ObtainTokenDTO;
|
import com.honeycombis.honeycom.common.security.dto.ObtainTokenDTO;
|
||||||
|
import com.honeycombis.honeycom.common.security.service.HoneycomUser;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.cache.CacheManager;
|
import org.springframework.cache.CacheManager;
|
||||||
import org.springframework.security.authentication.event.LogoutSuccessEvent;
|
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.OAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
import org.springframework.security.oauth2.core.OAuth2RefreshToken;
|
||||||
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
|
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.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author sunan
|
* @author sunan
|
||||||
*/
|
*/
|
||||||
@@ -77,8 +83,15 @@ public class HoneycomCustomerService {
|
|||||||
// 清空access token
|
// 清空access token
|
||||||
authorizationService.remove(authorization);
|
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(
|
SpringContextHolder.publishEvent(new LogoutSuccessEvent(new PreAuthenticatedAuthenticationToken(
|
||||||
authorization.getPrincipalName(), authorization.getRegisteredClientId())));
|
honeycomUser.getId(), authorization.getRegisteredClientId())));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,11 @@
|
|||||||
package com.honeycombis.honeycom.auth.support.handler;
|
package com.honeycombis.honeycom.auth.support.handler;
|
||||||
|
|
||||||
import com.honeycombis.honeycom.admin.api.dto.SysLogDTO;
|
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.core.util.WebUtils;
|
||||||
import com.honeycombis.honeycom.common.log.util.SysLogUtils;
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
@@ -37,6 +40,9 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<LogoutSuccessEvent> {
|
public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<LogoutSuccessEvent> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RemoteHoneyLogService remoteLogService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(LogoutSuccessEvent event) {
|
public void onApplicationEvent(LogoutSuccessEvent event) {
|
||||||
Authentication authentication = (Authentication) event.getSource();
|
Authentication authentication = (Authentication) event.getSource();
|
||||||
@@ -54,7 +60,7 @@ public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<Lo
|
|||||||
public void handle(Authentication authentication) {
|
public void handle(Authentication authentication) {
|
||||||
log.info("用户:{} 退出成功", authentication.getPrincipal());
|
log.info("用户:{} 退出成功", authentication.getPrincipal());
|
||||||
SysLogDTO logVo = SysLogUtils.getSysLog();
|
SysLogDTO logVo = SysLogUtils.getSysLog();
|
||||||
logVo.setTitle("退出成功");
|
logVo.setTitle("退出登录");
|
||||||
// 发送异步日志事件
|
// 发送异步日志事件
|
||||||
Long startTime = System.currentTimeMillis();
|
Long startTime = System.currentTimeMillis();
|
||||||
Long endTime = System.currentTimeMillis();
|
Long endTime = System.currentTimeMillis();
|
||||||
@@ -67,7 +73,8 @@ public class HoneycomLogoutSuccessEventHandler implements ApplicationListener<Lo
|
|||||||
if (authentication instanceof PreAuthenticatedAuthenticationToken) {
|
if (authentication instanceof PreAuthenticatedAuthenticationToken) {
|
||||||
logVo.setServiceId(authentication.getCredentials().toString());
|
logVo.setServiceId(authentication.getCredentials().toString());
|
||||||
}
|
}
|
||||||
logVo.setCreateBy(authentication.getName());
|
logVo.setCreateBy(String.valueOf(authentication.getPrincipal()));
|
||||||
|
remoteLogService.saveLog(logVo, SecurityConstants.FROM_IN);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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.data.tenant.TenantContextHolder;
|
||||||
import com.honeycombis.honeycom.common.log.util.LogTypeEnum;
|
import com.honeycombis.honeycom.common.log.util.LogTypeEnum;
|
||||||
import com.honeycombis.honeycom.log.api.entity.SysLog;
|
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.api.vo.PreLogVO;
|
||||||
import com.honeycombis.honeycom.log.mapper.SysLogMapper;
|
import com.honeycombis.honeycom.log.mapper.SysLogMapper;
|
||||||
import com.honeycombis.honeycom.log.service.SysLogService;
|
import com.honeycombis.honeycom.log.service.SysLogService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@@ -33,8 +35,11 @@ import java.util.stream.Collectors;
|
|||||||
* @since 2017-11-20
|
* @since 2017-11-20
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
|
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements SysLogService {
|
||||||
|
|
||||||
|
private final RemoteSpdmLogService remoteSpdmLogService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量插入前端错误日志
|
* 批量插入前端错误日志
|
||||||
* @param preLogVoList 日志信息
|
* @param preLogVoList 日志信息
|
||||||
@@ -86,6 +91,12 @@ public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> impleme
|
|||||||
BeanUtils.copyProperties(sysLog, log, "createTime");
|
BeanUtils.copyProperties(sysLog, log, "createTime");
|
||||||
return baseMapper.insert(log);
|
return baseMapper.insert(log);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 退出登录记录到spdm日志中去
|
||||||
|
if ("退出登录".equals(sysLog.getTitle())) {
|
||||||
|
remoteSpdmLogService.saveLog(sysLog);
|
||||||
|
}
|
||||||
|
|
||||||
return Boolean.TRUE;
|
return Boolean.TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class SpdmService {
|
|||||||
public void asyncSaveLog(SysLogDto sysLogDto) {
|
public void asyncSaveLog(SysLogDto sysLogDto) {
|
||||||
try {
|
try {
|
||||||
SysLogDto sysLog = new SysLogDto();
|
SysLogDto sysLog = new SysLogDto();
|
||||||
sysLog.setTitle("登录成功");
|
sysLog.setTitle(sysLogDto.getTitle());
|
||||||
sysLog.setServiceId("simulation-system");
|
sysLog.setServiceId("simulation-system");
|
||||||
sysLog.setTenantId(sysLogDto.getTenantId());
|
sysLog.setTenantId(sysLogDto.getTenantId());
|
||||||
sysLog.setCreateBy(sysLogDto.getCreateBy());
|
sysLog.setCreateBy(sysLogDto.getCreateBy());
|
||||||
|
|||||||
Reference in New Issue
Block a user