refactor: Fix some code smells #26

Merged
jank merged 6 commits from refactor/fix-smells into main 2024-10-02 07:48:33 +00:00
3 changed files with 8 additions and 10 deletions

@ -21,7 +21,7 @@ import java.util.Date;
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class) @ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<?> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) { public ResponseEntity<ErrorDetails> handleHelloEntityNotFoundException(ResourceNotFoundException ex, WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false)); ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND); return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
} }

@ -83,9 +83,9 @@ class KeycloakSecurityConfig {
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> { jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwt -> {
List<GrantedAuthority> grantedAuthorities = new ArrayList<>(); List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
Map<String, Object> realmAccess = jwt.getClaim("realm_access"); Map<String, Object> realmAccess = jwt.getClaim(REALM_ACCESS_CLAIM);
if (realmAccess != null && realmAccess.containsKey("roles")) { if (realmAccess != null && realmAccess.containsKey(ROLES_CLAIM)) {
List<String> roles = (List<String>) realmAccess.get("roles"); List<String> roles = (List<String>) realmAccess.get(ROLES_CLAIM);
for (String role : roles) { for (String role : roles) {
grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role)); grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_" + role));
} }

@ -1,14 +1,12 @@
package de.szut.lf8_starter.welcome; package de.szut.lf8_starter.welcome;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication; 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.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.security.Principal; import java.util.Collection;
@RestController @RestController
public class WelcomeController { public class WelcomeController {
@ -19,8 +17,8 @@ public class WelcomeController {
} }
@GetMapping("/roles") @GetMapping("/roles")
public ResponseEntity<?> getRoles(Authentication authentication) { public ResponseEntity<Collection<GrantedAuthority>> getRoles(Authentication authentication) {
return ResponseEntity.ok(authentication.getAuthorities()); return ResponseEntity.ok((Collection<GrantedAuthority>) authentication.getAuthorities());
} }