Skip to content

Commit 9350e85

Browse files
author
Daenges
committed
Sample secured endpoint
1 parent 7eb76cd commit 9350e85

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

server/src/main/java/com/studybuddies/server/configuration/JwtAuthConverter.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,27 @@ public AbstractAuthenticationToken convert(Jwt source) {
3333
private Collection<? extends GrantedAuthority> extractRoles(Jwt jwt) {
3434
Set<String> roles = new HashSet<>();
3535

36+
// Extract roles from realm_access (if available)
3637
Map<String, Object> realmAccess = jwt.getClaimAsMap("realm_access");
37-
if(realmAccess != null && realmAccess.containsKey("roles")) {
38+
if (realmAccess != null && realmAccess.containsKey("roles")) {
3839
roles.addAll((Collection<? extends String>) realmAccess.get("roles"));
3940
}
4041

41-
Map<String, Object> resourceAccess = jwt.getClaim("resource_access");
42-
if(resourceAccess != null && resourceAccess.containsKey("demo")) {
43-
Map<String, Object> demoAccess = (Map<String, Object>) resourceAccess.get("roles");
44-
if(demoAccess != null && demoAccess.containsKey("roles")) {
45-
roles.addAll((Collection<? extends String>) demoAccess.get("roles"));
42+
// Extract roles from resource_access dynamically
43+
Map<String, Object> resourceAccess = jwt.getClaimAsMap("resource_access");
44+
if (resourceAccess != null) {
45+
for (Map.Entry<String, Object> entry : resourceAccess.entrySet()) {
46+
Map<String, Object> resource = (Map<String, Object>) entry.getValue();
47+
if (resource.containsKey("roles")) {
48+
roles.addAll((Collection<? extends String>) resource.get("roles"));
49+
}
4650
}
47-
4851
}
49-
System.out.println(roles);
50-
return roles.stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role.toUpperCase())).collect(Collectors.toSet());
52+
53+
// Convert to Spring Security GrantedAuthorities with "ROLE_" prefix
54+
return roles.stream()
55+
.map(role -> new SimpleGrantedAuthority("ROLE_" + role.toUpperCase()))
56+
.collect(Collectors.toSet());
5157
}
58+
5259
}

server/src/main/java/com/studybuddies/server/configuration/SecurityConfig.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.context.annotation.Bean;
55
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
67
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
78
import org.springframework.security.web.SecurityFilterChain;
89

910
@Configuration
11+
@EnableMethodSecurity
1012
public class SecurityConfig {
1113

1214
@Autowired
@@ -15,8 +17,7 @@ public class SecurityConfig {
1517
@Bean
1618
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
1719
http.
18-
authorizeHttpRequests(a -> a
19-
.anyRequest().authenticated())
20+
authorizeHttpRequests(auth -> auth.anyRequest().authenticated())
2021
.oauth2ResourceServer(oauth2 -> oauth2.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthConverter)));
2122
return http.build();
2223
}

server/src/main/java/com/studybuddies/server/web/MeetingController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.springframework.http.HttpHeaders;
1010
import org.springframework.http.HttpStatus;
1111
import org.springframework.http.ResponseEntity;
12+
import org.springframework.security.access.prepost.PreAuthorize;
1213
import org.springframework.web.bind.annotation.*;
1314

1415
@AllArgsConstructor
@@ -32,6 +33,7 @@ public ResponseEntity<?> changeMeeting(@RequestParam Long id, @Valid @RequestBod
3233
}
3334

3435
@GetMapping
36+
@PreAuthorize("hasRole('ADMIN')")
3537
public ResponseEntity<?> getMeeting(@RequestParam(required = false) Long id) {
3638
String response = meetingService.retrieveMeetingFromDatabase(id);
3739
return ResponseEntity.status(HttpStatus.OK).body(response);

0 commit comments

Comments
 (0)