fix(security): authentication bypass via non-null default userdetail on auth failure

When `getSubject()` returns null (e.g., no Shiro session, expired token, or exception) or when `getPrincipal()` is null, the method returns `new UserDetail()` instead of null. This means callers performing null-check guards (e.g., `if (SecurityUser.getUser() != null)`) will believe an authenticated user exists when none does. Additionally, `getSubject()` silently swallows all exceptions and returns null, masking authentication infrastructure failures. Any downstream code relying on a null return to deny access will be bypassed with a default UserDetail object.


Affected files: SecurityUser.java

Signed-off-by: kumburovicbranko682-boop <295886834+kumburovicbranko682-boop@users.noreply.github.com>
This commit is contained in:
kumburovicbranko682-boop 2026-06-27 19:38:43 +08:00
parent 06b0ff139b
commit 61675f423b

View file

@ -26,25 +26,27 @@ public class SecurityUser {
public static UserDetail getUser() { public static UserDetail getUser() {
Subject subject = getSubject(); Subject subject = getSubject();
if (subject == null) { if (subject == null) {
return new UserDetail(); return null;
} }
UserDetail user = (UserDetail) subject.getPrincipal(); UserDetail user = (UserDetail) subject.getPrincipal();
if (user == null) { if (user == null) {
return new UserDetail(); return null;
} }
return user; return user;
} }
public static String getToken() { public static String getToken() {
return getUser().getToken(); UserDetail user = getUser();
return user != null ? user.getToken() : null;
} }
/** /**
* 获取用户ID * 获取用户ID
*/ */
public static Long getUserId() { public static Long getUserId() {
return getUser().getId(); UserDetail user = getUser();
return user != null ? user.getId() : null;
} }
} }