9
src/main/java/dev/mars3142/fhq/client/AuthClient.java
Normal file
9
src/main/java/dev/mars3142/fhq/client/AuthClient.java
Normal 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();
|
||||
}
|
@@ -1,6 +1,9 @@
|
||||
package dev.mars3142.fhq.config;
|
||||
|
||||
import dev.mars3142.fhq.client.AuthClient;
|
||||
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.ClientHttpRequestFactorySettings;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
@@ -17,22 +20,34 @@ public class RestClientConfig {
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
RestClient.Builder restClientBuilder() {
|
||||
return RestClient.builder();
|
||||
return RestClient.builder()
|
||||
.requestInterceptor(new AuthInterceptor());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public TimeZoneClient timeZoneClient(RestClient.Builder restClientBuilder) {
|
||||
RestClient restClient = restClientBuilder
|
||||
public AuthClient authClient(RestClient.Builder builder) {
|
||||
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")
|
||||
.requestFactory(getClientRequestFactory())
|
||||
.build();
|
||||
var restClientAdapter = RestClientAdapter.create(restClient);
|
||||
var httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
|
||||
val restClientAdapter = RestClientAdapter.create(restClient);
|
||||
val httpServiceProxyFactory = HttpServiceProxyFactory.builderFor(restClientAdapter).build();
|
||||
return httpServiceProxyFactory.createClient(TimeZoneClient.class);
|
||||
}
|
||||
|
||||
private ClientHttpRequestFactory getClientRequestFactory() {
|
||||
ClientHttpRequestFactorySettings clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS;
|
||||
val clientHttpRequestFactorySettings = ClientHttpRequestFactorySettings.DEFAULTS;
|
||||
return ClientHttpRequestFactories.get(clientHttpRequestFactorySettings);
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user