From 61d72684c99fc6e89e40e71ca22a9ae4bd83a092 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:09:26 +0200 Subject: [PATCH 1/6] refactor: change response entity type in exception handler --- .../lf8_starter/exceptionHandling/GlobalExceptionHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java index 68a15a7..8ee2aaf 100644 --- a/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java +++ b/src/main/java/de/szut/lf8_starter/exceptionHandling/GlobalExceptionHandler.java @@ -21,7 +21,7 @@ import java.util.Date; public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) - public ResponseEntity handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) { + public ResponseEntity handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) { ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND); } -- 2.45.2 From 8e7b0770de3e0e6b623d2db61b68b28ee99de4cc Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:30:18 +0200 Subject: [PATCH 2/6] refactor(security): replace string literal with constant key --- .../de/szut/lf8_starter/security/KeycloakSecurityConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java index 8ab3fd8..c37b0bf 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java @@ -83,7 +83,7 @@ class KeycloakSecurityConfig { jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> { List grantedAuthorities = new ArrayList<>(); - Map realmAccess = jwt.getClaim("realm_access"); + Map realmAccess = jwt.getClaim(REALM_ACCESS_CLAIM); if (realmAccess != null && realmAccess.containsKey("roles")) { List roles = (List) realmAccess.get("roles"); for (String role : roles) { -- 2.45.2 From 993387fa0c965606986e4cbc3174b84af432c6d7 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:34:26 +0200 Subject: [PATCH 3/6] fix(security): update roles claim for granted authorities --- .../de/szut/lf8_starter/security/KeycloakSecurityConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java index c37b0bf..7ff32cc 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java @@ -84,8 +84,8 @@ class KeycloakSecurityConfig { List grantedAuthorities = new ArrayList<>(); Map realmAccess = jwt.getClaim(REALM_ACCESS_CLAIM); - if (realmAccess != null && realmAccess.containsKey("roles")) { - List roles = (List) realmAccess.get("roles"); + if (realmAccess != null && realmAccess.containsKey(ROLES_CLAIM)) { + List roles = (List) realmAccess.get(ROLES_CLAIM); for (String role : roles) { grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role)); } -- 2.45.2 From 9824fbbbfd68c394ba7bea768ec8d55a9fd4dad6 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:35:12 +0200 Subject: [PATCH 4/6] refactor(welcome): update getRoles method signature --- .../java/de/szut/lf8_starter/welcome/WelcomeController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java index 6ed067b..a565f3f 100644 --- a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java +++ b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java @@ -3,12 +3,12 @@ package de.szut.lf8_starter.welcome; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import java.security.Principal; +import java.util.Collection; @RestController public class WelcomeController { @@ -19,7 +19,7 @@ public class WelcomeController { } @GetMapping("/roles") - public ResponseEntity getRoles(Authentication authentication) { + public ResponseEntity> getRoles(Authentication authentication) { return ResponseEntity.ok(authentication.getAuthorities()); } -- 2.45.2 From e5d0bcb84e409ec306aaad37e25ef32f5f8f9fe5 Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:39:42 +0200 Subject: [PATCH 5/6] refactor(welcome): remove generic type from getRoles method --- .../java/de/szut/lf8_starter/welcome/WelcomeController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java index a565f3f..0cfb9e5 100644 --- a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java +++ b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java @@ -19,7 +19,7 @@ public class WelcomeController { } @GetMapping("/roles") - public ResponseEntity> getRoles(Authentication authentication) { + public ResponseEntity getRoles(Authentication authentication) { return ResponseEntity.ok(authentication.getAuthorities()); } -- 2.45.2 From 54a76de08cdbc8fa5f50f9fa8c6d1590777b16cf Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 09:45:08 +0200 Subject: [PATCH 6/6] style: Remove unnecessary blank lines and improve type safety --- .../java/de/szut/lf8_starter/welcome/WelcomeController.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java index 0cfb9e5..4effa6b 100644 --- a/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java +++ b/src/main/java/de/szut/lf8_starter/welcome/WelcomeController.java @@ -1,7 +1,5 @@ package de.szut.lf8_starter.welcome; - - import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; @@ -19,8 +17,8 @@ public class WelcomeController { } @GetMapping("/roles") - public ResponseEntity getRoles(Authentication authentication) { - return ResponseEntity.ok(authentication.getAuthorities()); + public ResponseEntity> getRoles(Authentication authentication) { + return ResponseEntity.ok((Collection) authentication.getAuthorities()); } -- 2.45.2