Skip to content

Commit 9721fa7

Browse files
committed
retry to generate token via keycloak
1 parent 86f41b1 commit 9721fa7

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

grpc-spring-boot-starter-demo/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ dependencies {
6969

7070
implementation 'org.springframework.boot:spring-boot-starter-web'
7171

72+
implementation "org.springframework.retry:spring-retry"
7273
implementation "org.springframework.security:spring-security-config"
7374
implementation "org.springframework.security:spring-security-oauth2-jose"
7475
implementation "org.springframework.security:spring-security-oauth2-resource-server"

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/JwtAuthBaseTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,31 @@
33

44
import io.grpc.Channel;
55
import io.grpc.ClientInterceptors;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.lognet.springboot.grpc.GrpcServerTestBase;
78
import org.lognet.springboot.grpc.security.AuthClientInterceptor;
89
import org.lognet.springboot.grpc.security.AuthHeader;
10+
import org.slf4j.Logger;
911
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.cloud.config.client.RetryProperties;
13+
import org.springframework.cloud.config.client.RetryTemplateFactory;
1014
import org.springframework.http.HttpEntity;
1115
import org.springframework.http.HttpHeaders;
1216
import org.springframework.http.MediaType;
1317
import org.springframework.http.ResponseEntity;
18+
import org.springframework.retry.support.RetryTemplate;
19+
import org.springframework.retry.support.RetryTemplateBuilder;
1420
import org.springframework.util.LinkedMultiValueMap;
1521
import org.springframework.web.client.RestTemplate;
1622
import org.springframework.web.util.DefaultUriBuilderFactory;
1723
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;
1824
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;
1925

2026
import java.nio.ByteBuffer;
27+
import java.util.Optional;
2128
import java.util.UUID;
2229

30+
@Slf4j
2331
public abstract class JwtAuthBaseTest extends GrpcServerTestBase {
2432

2533
private boolean globalSecuredChannel = false;
@@ -40,15 +48,17 @@ protected Channel getChannel() {
4048
return getChannel(globalSecuredChannel);
4149

4250
}
43-
protected Channel getChannel(boolean authenticated){
44-
return authenticated ? ClientInterceptors.intercept(super.getChannel(), getAuthClientInterceptor())
45-
: super.getChannel();
51+
52+
protected Channel getChannel(boolean authenticated) {
53+
return authenticated ? ClientInterceptors.intercept(super.getChannel(), getAuthClientInterceptor())
54+
: super.getChannel();
4655

4756
}
57+
4858
protected final static String USER_NAME = "keycloak-test";
4959

5060

51-
protected AuthClientInterceptor getAuthClientInterceptor() {
61+
protected AuthClientInterceptor getAuthClientInterceptor() {
5262
return new AuthClientInterceptor(
5363
AuthHeader.builder().bearer().tokenSupplier(this::generateToken));
5464
}
@@ -73,13 +83,25 @@ protected ByteBuffer generateToken() {
7383

7484
final RestTemplate restTemplate = new RestTemplate();
7585
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(authServerUrl));
76-
final ResponseEntity<String> response = restTemplate
77-
.postForEntity("/realms/test-realm/protocol/openid-connect/token", new HttpEntity<>(req, headers), String.class);
86+
87+
88+
89+
7890
try {
91+
final ResponseEntity<String> response = RetryTemplate.builder()
92+
.exponentialBackoff(300,1.5,3000)
93+
.build()
94+
.execute(ctx -> {
95+
Optional.ofNullable(ctx.getLastThrowable())
96+
.ifPresent(e -> log.info("Retrying on ...", e));
97+
return restTemplate
98+
.postForEntity("/realms/test-realm/protocol/openid-connect/token", new HttpEntity<>(req, headers), String.class);
99+
});
79100
return ByteBuffer.wrap(new ObjectMapper().readTree(response.getBody())
80101
.at("/access_token")
81102
.asText().getBytes());
82-
} catch ( Exception e) {
103+
} catch (Exception e) {
104+
log.error("Failed to generate token",e );
83105
throw new RuntimeException(e);
84106
}
85107
}

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/JwtRoleTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,7 @@ public void concurrencyTest() throws InterruptedException, ExecutionException {
106106
new Callable<Boolean>() {
107107
@Override
108108
public Boolean call() throws Exception {
109-
System.out.println("About to start call " + i);
110109
barrier.await();
111-
System.out.println("Start call " + i);
112110
try {
113111
if (i % 2 == 0) {
114112
shouldSucceed.incrementAndGet();
@@ -125,13 +123,12 @@ public Boolean call() throws Exception {
125123
} catch (Exception e) {
126124
return false;
127125
} finally {
128-
System.out.println("Call " + i + " finished");
129126
endCountDownLatch.countDown();
130127
}
131128
}
132129
})
133130
.map(executorService::submit)
134-
.collect(Collectors.toList());
131+
.toList();
135132

136133

137134
endCountDownLatch.await();

0 commit comments

Comments
 (0)