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 {
@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());
}