Refactor LoginController to use pattern matching for Jwt type check, simplify uuid extraction, and adjust debug logs.

This commit is contained in:
akastijn 2025-08-05 23:49:11 +02:00
parent 56f4ccf40e
commit d28b4a2b62

View File

@ -96,16 +96,15 @@ public class LoginController implements LoginApi {
log.debug("Loading username for logged in user"); log.debug("Loading username for logged in user");
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
log.debug("Loaded authentication for logged in user {}", authentication); log.debug("Loaded authentication for logged in user {}", authentication);
if (authentication == null || !(authentication.getPrincipal() instanceof OAuth2ResourceServerProperties.Jwt)) { if (authentication == null || !(authentication.getPrincipal() instanceof Jwt jwt)) {
log.debug("Loaded authentication for logged in user is null or not a jwt"); log.debug("Loaded authentication for logged in user is null or not a jwt");
return ResponseEntity.status(401).build(); return ResponseEntity.status(401).build();
} }
Jwt jwt = (Jwt) authentication.getPrincipal();
String stringUuid = jwt.getSubject(); String stringUuid = jwt.getSubject();
UUID uuid; UUID uuid;
try { try {
uuid = UUID.fromString(stringUuid); uuid = UUID.fromString(stringUuid);
log.debug("Loaded username for logged in user {}", uuid); log.debug("Loaded username for logged in user {}", uuid);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().build(); return ResponseEntity.badRequest().build();
} }