Skip to content

Commit

Permalink
retry to generate token via keycloak
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmlet committed Dec 29, 2022
1 parent 86f41b1 commit 9721fa7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions grpc-spring-boot-starter-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ dependencies {

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

implementation "org.springframework.retry:spring-retry"
implementation "org.springframework.security:spring-security-config"
implementation "org.springframework.security:spring-security-oauth2-jose"
implementation "org.springframework.security:spring-security-oauth2-resource-server"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@

import io.grpc.Channel;
import io.grpc.ClientInterceptors;
import lombok.extern.slf4j.Slf4j;
import org.lognet.springboot.grpc.GrpcServerTestBase;
import org.lognet.springboot.grpc.security.AuthClientInterceptor;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.config.client.RetryProperties;
import org.springframework.cloud.config.client.RetryTemplateFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.retry.support.RetryTemplateBuilder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

import java.nio.ByteBuffer;
import java.util.Optional;
import java.util.UUID;

@Slf4j
public abstract class JwtAuthBaseTest extends GrpcServerTestBase {

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

}
protected Channel getChannel(boolean authenticated){
return authenticated ? ClientInterceptors.intercept(super.getChannel(), getAuthClientInterceptor())
: super.getChannel();

protected Channel getChannel(boolean authenticated) {
return authenticated ? ClientInterceptors.intercept(super.getChannel(), getAuthClientInterceptor())
: super.getChannel();

}

protected final static String USER_NAME = "keycloak-test";


protected AuthClientInterceptor getAuthClientInterceptor() {
protected AuthClientInterceptor getAuthClientInterceptor() {
return new AuthClientInterceptor(
AuthHeader.builder().bearer().tokenSupplier(this::generateToken));
}
Expand All @@ -73,13 +83,25 @@ protected ByteBuffer generateToken() {

final RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(authServerUrl));
final ResponseEntity<String> response = restTemplate
.postForEntity("/realms/test-realm/protocol/openid-connect/token", new HttpEntity<>(req, headers), String.class);




try {
final ResponseEntity<String> response = RetryTemplate.builder()
.exponentialBackoff(300,1.5,3000)
.build()
.execute(ctx -> {
Optional.ofNullable(ctx.getLastThrowable())
.ifPresent(e -> log.info("Retrying on ...", e));
return restTemplate
.postForEntity("/realms/test-realm/protocol/openid-connect/token", new HttpEntity<>(req, headers), String.class);
});
return ByteBuffer.wrap(new ObjectMapper().readTree(response.getBody())
.at("/access_token")
.asText().getBytes());
} catch ( Exception e) {
} catch (Exception e) {
log.error("Failed to generate token",e );
throw new RuntimeException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ public void concurrencyTest() throws InterruptedException, ExecutionException {
new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
System.out.println("About to start call " + i);
barrier.await();
System.out.println("Start call " + i);
try {
if (i % 2 == 0) {
shouldSucceed.incrementAndGet();
Expand All @@ -125,13 +123,12 @@ public Boolean call() throws Exception {
} catch (Exception e) {
return false;
} finally {
System.out.println("Call " + i + " finished");
endCountDownLatch.countDown();
}
}
})
.map(executorService::submit)
.collect(Collectors.toList());
.toList();


endCountDownLatch.await();
Expand Down

0 comments on commit 9721fa7

Please # to comment.