Skip to content

Commit 4874161

Browse files
committed
JAVA-46416 - test fixes
1 parent 6686bb0 commit 4874161

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
package com.baeldung.keycloak.keycloaksoap;
22

3+
import java.util.Collection;
4+
import java.util.List;
5+
import java.util.Map;
6+
import java.util.stream.Collectors;
7+
38
import org.springframework.core.convert.converter.Converter;
49
import org.springframework.security.core.GrantedAuthority;
510
import org.springframework.security.core.authority.SimpleGrantedAuthority;
611
import org.springframework.security.oauth2.jwt.Jwt;
712

8-
import java.util.*;
9-
import java.util.stream.Collectors;
10-
1113
public class KeycloakRoleConverter implements Converter<Jwt, Collection<GrantedAuthority>> {
1214

15+
private final String clientId;
16+
17+
public KeycloakRoleConverter(String clientId) {
18+
this.clientId = clientId;
19+
}
20+
1321
@Override
22+
@SuppressWarnings("unchecked")
1423
public Collection<GrantedAuthority> convert(Jwt jwt) {
1524
Map<String, Object> resourceAccess = jwt.getClaim("resource_access");
16-
// Replace this with your actual client ID from Keycloak
17-
String clientId = "baeldung-soap-services";
1825

1926
Map<String, Object> client = (Map<String, Object>) resourceAccess.get(clientId);
2027
List<String> roles = (List<String>) client.get("roles");

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.keycloak.keycloaksoap;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
45
import org.springframework.context.annotation.Bean;
56
import org.springframework.context.annotation.Configuration;
@@ -15,6 +16,10 @@
1516
@ConditionalOnProperty(name = "keycloak.enabled", havingValue = "true")
1617
@EnableMethodSecurity(jsr250Enabled = true)
1718
public class KeycloakSecurityConfig {
19+
20+
@Value("${client.id}")
21+
private String clientId;
22+
1823
@Bean
1924
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2025
http.csrf(AbstractHttpConfigurer::disable)
@@ -29,7 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
2934
@Bean
3035
public JwtAuthenticationConverter jwtAuthenticationConverter() {
3136
JwtAuthenticationConverter converter = new JwtAuthenticationConverter();
32-
converter.setJwtGrantedAuthoritiesConverter(new KeycloakRoleConverter());
37+
converter.setJwtGrantedAuthoritiesConverter(new KeycloakRoleConverter(clientId));
3338
return converter;
3439
}
3540
}

spring-boot-modules/spring-boot-keycloak-2/src/test/java/com/baeldung/keycloaksoap/KeycloakSoapLiveTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ void givenWrongAccessToken_whenGetProducts_thenReturnError() {
9898
}
9999

100100
/**
101-
* Happy flow to test <i>deleteProduct</i> operation. Test the <i>jhondoe</i> user.
101+
* Happy flow to test <i>deleteProduct</i> operation. Test the <i>johndoe</i> user.
102102
* This user should be configured in Keycloak server with a role <i>user</i>
103103
*/
104104
@Test
105105
@DisplayName("Delete Product With Access Token")
106106
void givenAccessToken_whenDeleteProduct_thenReturnSuccess() {
107107
HttpHeaders headers = new HttpHeaders();
108108
headers.set("content-type", "text/xml");
109-
headers.set("Authorization", "Bearer " + generateToken("jhondoe", "password"));
109+
headers.set("Authorization", "Bearer " + generateToken("johndoe", "password"));
110110
HttpEntity<String> request = new HttpEntity<>(Utility.getDeleteProductsRequest(), headers);
111111
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:" + port + "/ws/api/v1/", request, String.class);
112112

@@ -126,7 +126,7 @@ void givenAccessToken_whenDeleteProduct_thenReturnSuccess() {
126126
void givenUnauthorizedAccessToken_whenDeleteProduct_thenReturnUnauthorized() {
127127
HttpHeaders headers = new HttpHeaders();
128128
headers.set("content-type", "text/xml");
129-
headers.set("Authorization", "Bearer " + generateToken("johndoe", "password"));
129+
headers.set("Authorization", "Bearer " + generateToken("janedoe", "password"));
130130
HttpEntity<String> request = new HttpEntity<>(Utility.getDeleteProductsRequest(), headers);
131131
ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://localhost:" + port + "/ws/api/v1/", request, String.class);
132132

0 commit comments

Comments
 (0)