33
44import io .grpc .Channel ;
55import io .grpc .ClientInterceptors ;
6+ import lombok .extern .slf4j .Slf4j ;
67import org .lognet .springboot .grpc .GrpcServerTestBase ;
78import org .lognet .springboot .grpc .security .AuthClientInterceptor ;
89import org .lognet .springboot .grpc .security .AuthHeader ;
10+ import org .slf4j .Logger ;
911import org .springframework .beans .factory .annotation .Value ;
12+ import org .springframework .cloud .config .client .RetryProperties ;
13+ import org .springframework .cloud .config .client .RetryTemplateFactory ;
1014import org .springframework .http .HttpEntity ;
1115import org .springframework .http .HttpHeaders ;
1216import org .springframework .http .MediaType ;
1317import org .springframework .http .ResponseEntity ;
18+ import org .springframework .retry .support .RetryTemplate ;
19+ import org .springframework .retry .support .RetryTemplateBuilder ;
1420import org .springframework .util .LinkedMultiValueMap ;
1521import org .springframework .web .client .RestTemplate ;
1622import org .springframework .web .util .DefaultUriBuilderFactory ;
1723import org .testcontainers .shaded .com .fasterxml .jackson .core .JsonProcessingException ;
1824import org .testcontainers .shaded .com .fasterxml .jackson .databind .ObjectMapper ;
1925
2026import java .nio .ByteBuffer ;
27+ import java .util .Optional ;
2128import java .util .UUID ;
2229
30+ @ Slf4j
2331public 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 }
0 commit comments