Commit d79bf7956cdbafd31d9574f672bf6e872473ae6b

Authored by YunaiV
1 parent ad54e58a

SecurityFrameworkUtils 返回用户信息时,增加判断逻辑,保证正确性

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 2  
3 3 import cn.iocoder.dashboard.framework.security.core.LoginUser;
4 4 import cn.iocoder.dashboard.framework.web.core.util.WebFrameworkUtils;
  5 +import org.springframework.lang.Nullable;
5 6 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  7 +import org.springframework.security.core.Authentication;
  8 +import org.springframework.security.core.context.SecurityContext;
6 9 import org.springframework.security.core.context.SecurityContextHolder;
7 10 import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
8 11 import org.springframework.util.StringUtils;
... ... @@ -40,9 +43,20 @@ public class SecurityFrameworkUtils {
40 43  
41 44 /**
42 45 * 获取当前用户
  46 + *
  47 + * @return 当前用户
43 48 */
  49 + @Nullable
44 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 64 *
51 65 * @return 用户编号
52 66 */
  67 + @Nullable
53 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 75 *
60 76 * @return 角色编号数组
61 77 */
  78 + @Nullable
62 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 162 public void logout(String token) {
163 163 // 查询用户信息
164 164 LoginUser loginUser = userSessionService.getLoginUser(token);
165   - if(loginUser == null) {
  165 + if (loginUser == null) {
166 166 return;
167 167 }
168   - // 删除session
  168 + // 删除 session
169 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 175 SysLoginLogCreateReqVO reqVO = new SysLoginLogCreateReqVO();
175 176 reqVO.setLogType(SysLoginLogTypeEnum.LOGOUT_SELF.getType());
176 177 reqVO.setTraceId(TracerUtils.getTraceId());
177 178 reqVO.setUsername(username);
178 179 reqVO.setUserAgent(ServletUtils.getUserAgent());
179 180 reqVO.setUserIp(ServletUtils.getClientIP());
180   - reqVO.setResult(loginResult.getResult());
  181 + reqVO.setResult(SysLoginResultEnum.SUCCESS.getResult());
181 182 loginLogService.createLoginLog(reqVO);
182 183 }
183 184  
... ...