refactor: Fix some code smells (!26)
All checks were successful
Quality Check / Tests (push) Successful in 1m1s
Quality Check / Checkstyle Main (push) Successful in 46s
Build / Build and analyze (push) Successful in 1m56s
Release / Release (push) Successful in 38s

Reviewed-on: #26
Reviewed-by: Phan Huy Tran <ptran@noreply.localhost>
Co-authored-by: Jan Klattenhoff <jan@kjan.de>
Co-committed-by: Jan Klattenhoff <jan@kjan.de>
This commit is contained in:
Jan Gleytenhoover 2024-10-02 07:48:32 +00:00 committed by Jan Gleytenhoover
parent c317b226fe
commit a04a180712
3 changed files with 8 additions and 10 deletions

@ -21,7 +21,7 @@ import java.util.Date;
public class GlobalExceptionHandler {
@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));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}

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

@ -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<Collection<GrantedAuthority>> getRoles(Authentication authentication) {
return ResponseEntity.ok((Collection<GrantedAuthority>) authentication.getAuthorities());
}