new cool stuff

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2024-11-01 22:51:03 +01:00
parent 4f3b03cec5
commit f170687cb8
3 changed files with 49 additions and 6 deletions

View File

@@ -0,0 +1,9 @@
package dev.mars3142.fhq.client;
import org.springframework.web.service.annotation.GetExchange;
public interface AuthClient {
@GetExchange("/v1/auth")
String getAuth();
}

View File

@@ -1,6 +1,9 @@
package dev.mars3142.fhq.config; package dev.mars3142.fhq.config;
import dev.mars3142.fhq.client.AuthClient;
import dev.mars3142.fhq.client.TimeZoneClient; import dev.mars3142.fhq.client.TimeZoneClient;
import dev.mars3142.fhq.config.interceptor.AuthInterceptor;
import lombok.val;
import org.springframework.boot.web.client.ClientHttpRequestFactories; import org.springframework.boot.web.client.ClientHttpRequestFactories;
import org.springframework.boot.web.client.ClientHttpRequestFactorySettings; import org.springframework.boot.web.client.ClientHttpRequestFactorySettings;
import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.client.loadbalancer.LoadBalanced;
@@ -17,22 +20,34 @@ public class RestClientConfig {
@LoadBalanced @LoadBalanced
@Bean @Bean
RestClient.Builder restClientBuilder() { RestClient.Builder restClientBuilder() {
return RestClient.builder(); return RestClient.builder()
.requestInterceptor(new AuthInterceptor());
} }
@Bean @Bean
public TimeZoneClient timeZoneClient(RestClient.Builder restClientBuilder) { public AuthClient authClient(RestClient.Builder builder) {
RestClient restClient = restClientBuilder val restClient = builder
.baseUrl("http://auth-service")
.requestFactory(getClientRequestFactory())
.build();
val restClientAdapter = RestClientAdapter.create(restClient);
val httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
return httpServiceProxyFactory.createClient(AuthClient.class);
}
@Bean
public TimeZoneClient timeZoneClient(RestClient.Builder builder) {
val restClient = builder
.baseUrl("http://timezone-service") .baseUrl("http://timezone-service")
.requestFactory(getClientRequestFactory()) .requestFactory(getClientRequestFactory())
.build(); .build();
var restClientAdapter = RestClientAdapter.create(restClient); val restClientAdapter = RestClientAdapter.create(restClient);
var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build(); val httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
return httpServiceProxyFactory.createClient(TimeZoneClient.class); return httpServiceProxyFactory.createClient(TimeZoneClient.class);
} }
private ClientHttpRequestFactory getClientRequestFactory() { private ClientHttpRequestFactory getClientRequestFactory() {
ClientHttpRequestFactorySettings clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS; val clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS;
return ClientHttpRequestFactories.get(clientHttpRequestFactorySettings); return ClientHttpRequestFactories.get(clientHttpRequestFactorySettings);
} }
} }

View File

@@ -0,0 +1,19 @@
package dev.mars3142.fhq.config.interceptor;
import java.io.IOException;
import lombok.val;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
public class AuthInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {
val headers = request.getHeaders();
headers.add("X-FHQ-USER-ID", "Vaadin");
return execution.execute(request, body);
}
}