From a04a1807120e790bd78943eb50a324771fa4346e Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Wed, 2 Oct 2024 07:48:32 +0000 Subject: [PATCH] refactor: Fix some code smells (!26) Reviewed-on: https://git.kjan.de/jank/LF8/pulls/26 Reviewed-by: Phan Huy Tran Co-authored-by: Jan Klattenhoff Co-committed-by: Jan Klattenhoff --- .../exceptionHandling/GlobalExceptionHandler.java | 2 +- .../lf8_starter/security/KeycloakSecurityConfig.java | 6 +++--- .../de/szut/lf8_starter/welcome/WelcomeController.java | 10 ++++------ 3 files changed, 8 insertions(+), 10 deletions(-) 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); } 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..7ff32cc 100644 --- a/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java +++ b/src/main/java/de/szut/lf8_starter/security/KeycloakSecurityConfig.java @@ -83,9 +83,9 @@ class KeycloakSecurityConfig { jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> { List grantedAuthorities = new ArrayList<>(); - Map realmAccess = jwt.getClaim("realm_access"); - if (realmAccess != null && realmAccess.containsKey("roles")) { - List roles = (List) realmAccess.get("roles"); + Map realmAccess = jwt.getClaim(REALM_ACCESS_CLAIM); + if (realmAccess != null && realmAccess.containsKey(ROLES_CLAIM)) { + List roles = (List) realmAccess.get(ROLES_CLAIM); for (String role : roles) { grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role)); } 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..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,14 +1,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,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()); }