From d79bf7956cdbafd31d9574f672bf6e872473ae6b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 9 Mar 2021 00:35:04 +0800 Subject: [PATCH] =?UTF-8?q?SecurityFrameworkUtils=20=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=E6=97=B6=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=88=A4=E6=96=AD=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BF=9D?= =?UTF-8?q?=E8=AF=81=E6=AD=A3=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/util/SecurityFrameworkUtils.java | 24 ++++++++++++++++--- .../service/auth/impl/SysAuthServiceImpl.java | 11 +++++---- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java b/src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java index eb67479d4..b6c509b93 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java +++ b/src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java @@ -2,7 +2,10 @@ package cn.iocoder.dashboard.framework.security.core.util; import cn.iocoder.dashboard.framework.security.core.LoginUser; import cn.iocoder.dashboard.framework.web.core.util.WebFrameworkUtils; +import org.springframework.lang.Nullable; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; import org.springframework.util.StringUtils; @@ -40,9 +43,20 @@ public class SecurityFrameworkUtils { /** * 获取当前用户 + * + * @return 当前用户 */ + @Nullable public static LoginUser getLoginUser() { - return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + SecurityContext context = SecurityContextHolder.getContext(); + if (context == null) { + return null; + } + Authentication authentication = context.getAuthentication(); + if (authentication == null) { + return null; + } + return (LoginUser) authentication.getPrincipal(); } /** @@ -50,8 +64,10 @@ public class SecurityFrameworkUtils { * * @return 用户编号 */ + @Nullable public static Long getLoginUserId() { - return getLoginUser().getId(); + LoginUser loginUser = getLoginUser(); + return loginUser != null ? loginUser.getId() : null; } /** @@ -59,8 +75,10 @@ public class SecurityFrameworkUtils { * * @return 角色编号数组 */ + @Nullable public static Set getLoginUserRoleIds() { - return getLoginUser().getRoleIds(); + LoginUser loginUser = getLoginUser(); + return loginUser != null ? loginUser.getRoleIds() : null; } /** diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java index 675d26a54..dc5b3be84 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -162,22 +162,23 @@ public class SysAuthServiceImpl implements SysAuthService { public void logout(String token) { // 查询用户信息 LoginUser loginUser = userSessionService.getLoginUser(token); - if(loginUser == null) { + if (loginUser == null) { return; } - // 删除session + // 删除 session userSessionService.deleteUserSession(token); - this.createLogoutLog(loginUser.getUsername(), SysLoginResultEnum.SUCCESS); + // 记录登出日子和 + this.createLogoutLog(loginUser.getUsername()); } - private void createLogoutLog(String username, SysLoginResultEnum loginResult) { + private void createLogoutLog(String username) { SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); reqVO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); reqVO.setTraceId(TracerUtils.getTraceId()); reqVO.setUsername(username); reqVO.setUserAgent(ServletUtils.getUserAgent()); reqVO.setUserIp(ServletUtils.getClientIP()); - reqVO.setResult(loginResult.getResult()); + reqVO.setResult(SysLoginResultEnum.SUCCESS.getResult()); loginLogService.createLoginLog(reqVO); }