🐛 登陆成功事件,日志保存用户名错误

This commit is contained in:
付绪壮 2022-08-12 19:15:42 +08:00
parent 83aced1a32
commit 1242a5958b

View File

@ -20,6 +20,7 @@ import com.pig4cloud.pig.admin.api.entity.SysLog;
import com.pig4cloud.pig.common.core.util.SpringContextHolder;
import com.pig4cloud.pig.common.log.event.SysLogEvent;
import com.pig4cloud.pig.common.log.util.SysLogUtils;
import com.pig4cloud.pig.common.security.service.PigUser;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageConverter;
@ -51,6 +52,7 @@ public class PigAuthenticationSuccessEventHandler implements AuthenticationSucce
/**
* Called when a user has been successfully authenticated.
*
* @param request the request which caused the successful authentication
* @param response the response
* @param authentication the <tt>Authentication</tt> object which was created during
@ -60,16 +62,21 @@ public class PigAuthenticationSuccessEventHandler implements AuthenticationSucce
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) {
log.info("用户:{} 登录成功", authentication.getPrincipal());
SecurityContextHolder.getContext().setAuthentication(authentication);
OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = (OAuth2AccessTokenAuthenticationToken) authentication;
Map<String, Object> map = accessTokenAuthentication.getAdditionalParameters();
PigUser userInfo = (PigUser) map.get("user_info");
log.info("用户:{} 登录成功", userInfo.getName());
SecurityContextHolder.getContext().setAuthentication(accessTokenAuthentication);
// 发送异步日志事件
SysLog logVo = SysLogUtils.getSysLog();
logVo.setTitle("登录成功");
// 发送异步日志事件
Long startTime = System.currentTimeMillis();
Long endTime = System.currentTimeMillis();
logVo.setTime(endTime - startTime);
logVo.setCreateBy(authentication.getName());
logVo.setUpdateBy(authentication.getName());
logVo.setCreateBy(userInfo.getName());
logVo.setUpdateBy(userInfo.getName());
SpringContextHolder.publishEvent(new SysLogEvent(logVo));
// 输出token