Commit d79bf7956cdbafd31d9574f672bf6e872473ae6b
1 parent
ad54e58a
SecurityFrameworkUtils 返回用户信息时,增加判断逻辑,保证正确性
Showing
2 changed files
with
27 additions
and
8 deletions
src/main/java/cn/iocoder/dashboard/framework/security/core/util/SecurityFrameworkUtils.java
| @@ -2,7 +2,10 @@ package cn.iocoder.dashboard.framework.security.core.util; | @@ -2,7 +2,10 @@ package cn.iocoder.dashboard.framework.security.core.util; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.dashboard.framework.security.core.LoginUser; | 3 | import cn.iocoder.dashboard.framework.security.core.LoginUser; |
| 4 | import cn.iocoder.dashboard.framework.web.core.util.WebFrameworkUtils; | 4 | import cn.iocoder.dashboard.framework.web.core.util.WebFrameworkUtils; |
| 5 | +import org.springframework.lang.Nullable; | ||
| 5 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | 6 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 7 | +import org.springframework.security.core.Authentication; | ||
| 8 | +import org.springframework.security.core.context.SecurityContext; | ||
| 6 | import org.springframework.security.core.context.SecurityContextHolder; | 9 | import org.springframework.security.core.context.SecurityContextHolder; |
| 7 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | 10 | import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; |
| 8 | import org.springframework.util.StringUtils; | 11 | import org.springframework.util.StringUtils; |
| @@ -40,9 +43,20 @@ public class SecurityFrameworkUtils { | @@ -40,9 +43,20 @@ public class SecurityFrameworkUtils { | ||
| 40 | 43 | ||
| 41 | /** | 44 | /** |
| 42 | * 获取当前用户 | 45 | * 获取当前用户 |
| 46 | + * | ||
| 47 | + * @return 当前用户 | ||
| 43 | */ | 48 | */ |
| 49 | + @Nullable | ||
| 44 | public static LoginUser getLoginUser() { | 50 | public static LoginUser getLoginUser() { |
| 45 | - return (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | 51 | + SecurityContext context = SecurityContextHolder.getContext(); |
| 52 | + if (context == null) { | ||
| 53 | + return null; | ||
| 54 | + } | ||
| 55 | + Authentication authentication = context.getAuthentication(); | ||
| 56 | + if (authentication == null) { | ||
| 57 | + return null; | ||
| 58 | + } | ||
| 59 | + return (LoginUser) authentication.getPrincipal(); | ||
| 46 | } | 60 | } |
| 47 | 61 | ||
| 48 | /** | 62 | /** |
| @@ -50,8 +64,10 @@ public class SecurityFrameworkUtils { | @@ -50,8 +64,10 @@ public class SecurityFrameworkUtils { | ||
| 50 | * | 64 | * |
| 51 | * @return 用户编号 | 65 | * @return 用户编号 |
| 52 | */ | 66 | */ |
| 67 | + @Nullable | ||
| 53 | public static Long getLoginUserId() { | 68 | public static Long getLoginUserId() { |
| 54 | - return getLoginUser().getId(); | 69 | + LoginUser loginUser = getLoginUser(); |
| 70 | + return loginUser != null ? loginUser.getId() : null; | ||
| 55 | } | 71 | } |
| 56 | 72 | ||
| 57 | /** | 73 | /** |
| @@ -59,8 +75,10 @@ public class SecurityFrameworkUtils { | @@ -59,8 +75,10 @@ public class SecurityFrameworkUtils { | ||
| 59 | * | 75 | * |
| 60 | * @return 角色编号数组 | 76 | * @return 角色编号数组 |
| 61 | */ | 77 | */ |
| 78 | + @Nullable | ||
| 62 | public static Set<Long> getLoginUserRoleIds() { | 79 | public static Set<Long> getLoginUserRoleIds() { |
| 63 | - return getLoginUser().getRoleIds(); | 80 | + LoginUser loginUser = getLoginUser(); |
| 81 | + return loginUser != null ? loginUser.getRoleIds() : null; | ||
| 64 | } | 82 | } |
| 65 | 83 | ||
| 66 | /** | 84 | /** |
src/main/java/cn/iocoder/dashboard/modules/system/service/auth/impl/SysAuthServiceImpl.java
| @@ -162,22 +162,23 @@ public class SysAuthServiceImpl implements SysAuthService { | @@ -162,22 +162,23 @@ public class SysAuthServiceImpl implements SysAuthService { | ||
| 162 | public void logout(String token) { | 162 | public void logout(String token) { |
| 163 | // 查询用户信息 | 163 | // 查询用户信息 |
| 164 | LoginUser loginUser = userSessionService.getLoginUser(token); | 164 | LoginUser loginUser = userSessionService.getLoginUser(token); |
| 165 | - if(loginUser == null) { | 165 | + if (loginUser == null) { |
| 166 | return; | 166 | return; |
| 167 | } | 167 | } |
| 168 | - // 删除session | 168 | + // 删除 session |
| 169 | userSessionService.deleteUserSession(token); | 169 | userSessionService.deleteUserSession(token); |
| 170 | - this.createLogoutLog(loginUser.getUsername(), SysLoginResultEnum.SUCCESS); | 170 | + // 记录登出日子和 |
| 171 | + this.createLogoutLog(loginUser.getUsername()); | ||
| 171 | } | 172 | } |
| 172 | 173 | ||
| 173 | - private void createLogoutLog(String username, SysLoginResultEnum loginResult) { | 174 | + private void createLogoutLog(String username) { |
| 174 | SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); | 175 | SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO(); |
| 175 | reqVO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); | 176 | reqVO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType()); |
| 176 | reqVO.setTraceId(TracerUtils.getTraceId()); | 177 | reqVO.setTraceId(TracerUtils.getTraceId()); |
| 177 | reqVO.setUsername(username); | 178 | reqVO.setUsername(username); |
| 178 | reqVO.setUserAgent(ServletUtils.getUserAgent()); | 179 | reqVO.setUserAgent(ServletUtils.getUserAgent()); |
| 179 | reqVO.setUserIp(ServletUtils.getClientIP()); | 180 | reqVO.setUserIp(ServletUtils.getClientIP()); |
| 180 | - reqVO.setResult(loginResult.getResult()); | 181 | + reqVO.setResult(SysLoginResultEnum.SUCCESS.getResult()); |
| 181 | loginLogService.createLoginLog(reqVO); | 182 | loginLogService.createLoginLog(reqVO); |
| 182 | } | 183 | } |
| 183 | 184 |