diff --git a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationSuccessEventHandler.java b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationSuccessEventHandler.java index a6142aac..5aaf9fa1 100644 --- a/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationSuccessEventHandler.java +++ b/pig-auth/src/main/java/com/pig4cloud/pig/auth/support/handler/PigAuthenticationSuccessEventHandler.java @@ -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,25 +52,31 @@ 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 request the request which caused the successful authentication + * @param response the response * @param authentication the Authentication object which was created during - * the authentication process. + * the authentication process. */ @SneakyThrows @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, - Authentication authentication) { - log.info("用户:{} 登录成功", authentication.getPrincipal()); - SecurityContextHolder.getContext().setAuthentication(authentication); + Authentication authentication) { + OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = (OAuth2AccessTokenAuthenticationToken) authentication; + Map 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 @@ -77,7 +84,7 @@ public class PigAuthenticationSuccessEventHandler implements AuthenticationSucce } private void sendAccessTokenResponse(HttpServletRequest request, HttpServletResponse response, - Authentication authentication) throws IOException { + Authentication authentication) throws IOException { OAuth2AccessTokenAuthenticationToken accessTokenAuthentication = (OAuth2AccessTokenAuthenticationToken) authentication;