|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.security.GeneralSecurityException; |
21 | | -import java.util.Arrays; |
| 21 | +import java.util.ArrayList; |
22 | 22 | import java.util.Base64; |
23 | 23 | import java.util.HashMap; |
| 24 | +import java.util.List; |
24 | 25 | import java.util.Map; |
25 | 26 | import java.util.concurrent.TimeUnit; |
26 | 27 |
|
|
35 | 36 | import org.apache.hc.core5.http.io.SocketConfig; |
36 | 37 | import org.apache.hc.core5.util.Timeout; |
37 | 38 |
|
| 39 | +import org.springframework.boot.security.oauth2.client.autoconfigure.OAuth2ClientProperties; |
| 40 | +import org.springframework.boot.security.oauth2.client.autoconfigure.OAuth2ClientPropertiesMapper; |
38 | 41 | import org.springframework.cloud.configuration.SSLContextFactory; |
39 | 42 | import org.springframework.http.HttpHeaders; |
40 | 43 | import org.springframework.http.HttpRequest; |
|
44 | 47 | import org.springframework.http.client.ClientHttpResponse; |
45 | 48 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
46 | 49 | import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 50 | +import org.springframework.security.oauth2.client.AuthorizedClientServiceOAuth2AuthorizedClientManager; |
| 51 | +import org.springframework.security.oauth2.client.InMemoryOAuth2AuthorizedClientService; |
| 52 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; |
| 53 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; |
| 54 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; |
| 55 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; |
| 56 | +import org.springframework.security.oauth2.client.registration.ClientRegistration; |
| 57 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 58 | +import org.springframework.security.oauth2.client.registration.InMemoryClientRegistrationRepository; |
| 59 | +import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor; |
47 | 60 | import org.springframework.web.client.RestTemplate; |
48 | 61 |
|
49 | 62 | import static org.springframework.cloud.config.client.ConfigClientProperties.AUTHORIZATION; |
@@ -77,15 +90,67 @@ public RestTemplate create() { |
77 | 90 |
|
78 | 91 | ClientHttpRequestFactory requestFactory = createHttpRequestFactory(properties); |
79 | 92 | RestTemplate template = new RestTemplate(requestFactory); |
| 93 | + |
| 94 | + final List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>(); |
80 | 95 | Map<String, String> headers = new HashMap<>(properties.getHeaders()); |
81 | 96 | headers.remove(AUTHORIZATION); // To avoid redundant addition of header |
82 | 97 | if (!headers.isEmpty()) { |
83 | | - template.setInterceptors(Arrays.asList(new GenericRequestHeaderInterceptor(headers))); |
| 98 | + interceptors.add(new GenericRequestHeaderInterceptor(headers)); |
| 99 | + } |
| 100 | + |
| 101 | + if (properties.getOauth2().isEnabled()) { |
| 102 | + ClientHttpRequestInterceptor oauth2Interceptor = createOauth2Interceptor(properties.getOauth2()); |
| 103 | + interceptors.add(oauth2Interceptor); |
84 | 104 | } |
| 105 | + template.setInterceptors(interceptors); |
85 | 106 |
|
86 | 107 | return template; |
87 | 108 | } |
88 | 109 |
|
| 110 | + private ClientHttpRequestInterceptor createOauth2Interceptor(ConfigClientProperties.OAuth2Properties properties) { |
| 111 | + final OAuth2AuthorizedClientManager authorizedClientManager = createAuthorizedClientManager(properties); |
| 112 | + OAuth2ClientHttpRequestInterceptor oauth2Interceptor = new OAuth2ClientHttpRequestInterceptor( |
| 113 | + authorizedClientManager); |
| 114 | + oauth2Interceptor |
| 115 | + .setClientRegistrationIdResolver(request -> ConfigClientProperties.OAuth2Properties.CLIENT_REGISTRATION_ID); |
| 116 | + return oauth2Interceptor; |
| 117 | + } |
| 118 | + |
| 119 | + private OAuth2AuthorizedClientManager createAuthorizedClientManager( |
| 120 | + ConfigClientProperties.OAuth2Properties properties) { |
| 121 | + |
| 122 | + OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder() |
| 123 | + .clientCredentials() |
| 124 | + .refreshToken() |
| 125 | + .build(); |
| 126 | + |
| 127 | + ClientRegistrationRepository clientRegistrationRepository = clientRegistrationRepository(properties); |
| 128 | + |
| 129 | + OAuth2AuthorizedClientService authorizedClientService = new InMemoryOAuth2AuthorizedClientService( |
| 130 | + clientRegistrationRepository); |
| 131 | + |
| 132 | + AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager( |
| 133 | + clientRegistrationRepository, authorizedClientService); |
| 134 | + authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 135 | + return authorizedClientManager; |
| 136 | + } |
| 137 | + |
| 138 | + private ClientRegistrationRepository clientRegistrationRepository( |
| 139 | + ConfigClientProperties.OAuth2Properties properties) { |
| 140 | + OAuth2ClientProperties oauth2ClientProperties = new OAuth2ClientProperties(); |
| 141 | + properties.getRegistration().setProvider(null); // In case it was set in config |
| 142 | + // properties |
| 143 | + oauth2ClientProperties.getRegistration() |
| 144 | + .put(ConfigClientProperties.OAuth2Properties.CLIENT_REGISTRATION_ID, properties.getRegistration()); |
| 145 | + oauth2ClientProperties.getProvider() |
| 146 | + .put(ConfigClientProperties.OAuth2Properties.CLIENT_REGISTRATION_ID, properties.getProvider()); |
| 147 | + oauth2ClientProperties.afterPropertiesSet(); |
| 148 | + |
| 149 | + List<ClientRegistration> registrations = new ArrayList<>( |
| 150 | + new OAuth2ClientPropertiesMapper(oauth2ClientProperties).asClientRegistrations().values()); |
| 151 | + return new InMemoryClientRegistrationRepository(registrations); |
| 152 | + } |
| 153 | + |
89 | 154 | protected ClientHttpRequestFactory createHttpRequestFactory(ConfigClientProperties client) { |
90 | 155 | if (client.getTls().isEnabled()) { |
91 | 156 | try { |
|
0 commit comments