-
Notifications
You must be signed in to change notification settings - Fork 22
/
GraphQLWebClientAutoConfiguration.java
81 lines (70 loc) · 3.6 KB
/
GraphQLWebClientAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package graphql.kickstart.spring.webclient.boot;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.security.oauth2.client.servlet.OAuth2ClientAutoConfiguration;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager;
import org.springframework.security.oauth2.client.InMemoryReactiveOAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.reactive.function.client.ServerOAuth2AuthorizedClientExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
@Slf4j
@Configuration
@RequiredArgsConstructor
@AutoConfigureAfter({JacksonAutoConfiguration.class, OAuth2ClientAutoConfiguration.class,
WebFluxAutoConfiguration.class})
@EnableConfigurationProperties(GraphQLClientProperties.class)
@ComponentScan(basePackageClasses = GraphQLWebClientImpl.class)
public class GraphQLWebClientAutoConfiguration {
private final GraphQLClientProperties graphqlClientProperties;
@Bean
@ConditionalOnMissingBean
public WebClient webClient(
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") WebClient.Builder clientBuilder,
@Autowired(required = false) ReactiveClientRegistrationRepository clientRegistrations
) {
clientBuilder.baseUrl(graphqlClientProperties.getUrl());
if (isGraphQLClientRegistrationPresent(clientRegistrations)) {
clientBuilder.filter(createOAuthFilter(clientRegistrations));
}
return clientBuilder.build();
}
@NonNull
private ServerOAuth2AuthorizedClientExchangeFilterFunction createOAuthFilter(
ReactiveClientRegistrationRepository clientRegistrations) {
InMemoryReactiveOAuth2AuthorizedClientService clientService =
new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager =
new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations,
clientService);
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauth.setDefaultClientRegistrationId("graphql");
return oauth;
}
private boolean isGraphQLClientRegistrationPresent(
ReactiveClientRegistrationRepository clientRegistrations) {
return clientRegistrations != null && clientRegistrations.findByRegistrationId("graphql")
.blockOptional().isPresent();
}
@Bean
@ConditionalOnMissingBean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
@Bean
@ConditionalOnMissingBean
public GraphQLWebClient graphQLWebClient(WebClient webClient, ObjectMapper objectMapper) {
return new GraphQLWebClientImpl(webClient, objectMapper);
}
}