Skip to content

Commit f2d8c13

Browse files
committed
JAVA-45845: Changes made for POM Properties Cleanup
1 parent 735a180 commit f2d8c13

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.keycloak.keycloaksoap;
2+
3+
import org.springframework.core.convert.converter.Converter;
4+
import org.springframework.security.core.GrantedAuthority;
5+
import org.springframework.security.core.authority.SimpleGrantedAuthority;
6+
import org.springframework.security.oauth2.jwt.Jwt;
7+
8+
import java.util.*;
9+
import java.util.stream.Collectors;
10+
11+
public class KeycloakRoleConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
12+
13+
@Override
14+
public Collection<GrantedAuthority> convert(Jwt jwt) {
15+
Collection<GrantedAuthority> authorities = new ArrayList<>();
16+
// Extract client roles with ROLE_ prefix
17+
authorities.addAll(extractClientRoles(jwt));
18+
19+
return authorities;
20+
}
21+
22+
23+
24+
private Collection<GrantedAuthority> extractClientRoles(Jwt jwt) {
25+
Map<String, Object> resourceAccess = jwt.getClaim("resource_access");
26+
if (resourceAccess == null || resourceAccess.isEmpty()) {
27+
return Collections.emptyList();
28+
}
29+
// Replace this with your actual client ID from Keycloak
30+
String clientId = "baeldung-soap-services";
31+
32+
Map<String, Object> client = (Map<String, Object>) resourceAccess.get(clientId);
33+
if (client == null || client.isEmpty()) {
34+
return Collections.emptyList();
35+
}
36+
@SuppressWarnings("unchecked")
37+
List<String> roles = (List<String>) client.get("roles");
38+
if (roles == null) {
39+
return Collections.emptyList();
40+
}
41+
return roles.stream()
42+
.map(role -> new SimpleGrantedAuthority("ROLE_" + role)) // Add ROLE_ prefix here
43+
.collect(Collectors.toList());
44+
}
45+
}

spring-boot-modules/spring-boot-keycloak-2/src/main/java/com/baeldung/keycloak/keycloaksoap/KeycloakSecurityConfig.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,31 @@
88
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
99
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1010
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
11+
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
12+
import org.springframework.security.oauth2.server.resource.web.authentication.BearerTokenAuthenticationFilter;
1113
import org.springframework.security.web.SecurityFilterChain;
14+
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1215

1316
@Configuration
1417
@EnableWebSecurity
1518
@ConditionalOnProperty(name = "keycloak.enabled", havingValue = "true")
1619
@EnableMethodSecurity(jsr250Enabled = true)
1720
public class KeycloakSecurityConfig {
18-
1921
@Bean
2022
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2123
http.csrf(AbstractHttpConfigurer::disable)
2224
.authorizeHttpRequests(auth -> auth.anyRequest()
2325
.authenticated())
2426
.oauth2ResourceServer(oauth2 -> oauth2
25-
.jwt(Customizer.withDefaults()));
27+
.jwt(jwt -> jwt.jwtAuthenticationConverter(jwtAuthenticationConverter()))
28+
);
2629
return http.build();
2730
}
31+
32+
@Bean
33+
public JwtAuthenticationConverter jwtAuthenticationConverter() {
34+
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
35+
converter.setJwtGrantedAuthoritiesConverter(new KeycloakRoleConverter());
36+
return converter;
37+
}
2838
}

0 commit comments

Comments
 (0)