|
28 | 28 | import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; |
29 | 29 | import org.apache.hc.core5.http.config.Registry; |
30 | 30 | import org.apache.hc.core5.http.config.RegistryBuilder; |
| 31 | +import sample.authorization.DeviceCodeOAuth2AuthorizedClientProvider; |
31 | 32 |
|
| 33 | +import org.springframework.beans.factory.annotation.Qualifier; |
32 | 34 | import org.springframework.boot.ssl.SslBundle; |
33 | 35 | import org.springframework.boot.ssl.SslBundles; |
34 | 36 | import org.springframework.context.annotation.Bean; |
35 | 37 | import org.springframework.context.annotation.Configuration; |
36 | 38 | import org.springframework.http.client.ClientHttpRequestFactory; |
37 | 39 | import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 40 | +import org.springframework.http.converter.FormHttpMessageConverter; |
| 41 | +import org.springframework.security.oauth2.client.OAuth2AuthorizationFailureHandler; |
| 42 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; |
| 43 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider; |
| 44 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder; |
| 45 | +import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient; |
| 46 | +import org.springframework.security.oauth2.client.endpoint.OAuth2ClientCredentialsGrantRequest; |
| 47 | +import org.springframework.security.oauth2.client.endpoint.RestClientClientCredentialsTokenResponseClient; |
| 48 | +import org.springframework.security.oauth2.client.http.OAuth2ErrorResponseErrorHandler; |
| 49 | +import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; |
| 50 | +import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager; |
| 51 | +import org.springframework.security.oauth2.client.web.OAuth2AuthorizedClientRepository; |
| 52 | +import org.springframework.security.oauth2.client.web.client.OAuth2ClientHttpRequestInterceptor; |
| 53 | +import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
| 54 | +import org.springframework.security.oauth2.core.http.converter.OAuth2AccessTokenResponseHttpMessageConverter; |
| 55 | +import org.springframework.util.LinkedMultiValueMap; |
| 56 | +import org.springframework.util.MultiValueMap; |
| 57 | +import org.springframework.web.client.RestClient; |
38 | 58 |
|
39 | 59 | /** |
40 | 60 | * @author Joe Grandja |
|
43 | 63 | @Configuration(proxyBeanMethods = false) |
44 | 64 | public class RestClientConfig { |
45 | 65 |
|
| 66 | + @Bean("default-client-rest-client") |
| 67 | + public RestClient defaultClientRestClient( |
| 68 | + OAuth2AuthorizedClientRepository authorizedClientRepository, |
| 69 | + OAuth2AuthorizedClientManager authorizedClientManager, |
| 70 | + @Qualifier("default-client-http-request-factory") Supplier<ClientHttpRequestFactory> clientHttpRequestFactory) { |
| 71 | + |
| 72 | + OAuth2ClientHttpRequestInterceptor requestInterceptor = |
| 73 | + new OAuth2ClientHttpRequestInterceptor(authorizedClientManager); |
| 74 | + OAuth2AuthorizationFailureHandler authorizationFailureHandler = |
| 75 | + OAuth2ClientHttpRequestInterceptor.authorizationFailureHandler(authorizedClientRepository); |
| 76 | + requestInterceptor.setAuthorizationFailureHandler(authorizationFailureHandler); |
| 77 | + // @formatter:off |
| 78 | + return RestClient.builder() |
| 79 | + .requestFactory(clientHttpRequestFactory.get()) |
| 80 | + .requestInterceptor(requestInterceptor) |
| 81 | + .build(); |
| 82 | + // @formatter:on |
| 83 | + } |
| 84 | + |
| 85 | + @Bean("self-signed-demo-client-rest-client") |
| 86 | + public RestClient selfSignedDemoClientRestClient( |
| 87 | + ClientRegistrationRepository clientRegistrationRepository, |
| 88 | + OAuth2AuthorizedClientRepository authorizedClientRepository, |
| 89 | + @Qualifier("self-signed-demo-client-http-request-factory") Supplier<ClientHttpRequestFactory> clientHttpRequestFactory) { |
| 90 | + |
| 91 | + RestClient restClient = accessTokenRestClient(clientHttpRequestFactory); |
| 92 | + |
| 93 | + // @formatter:off |
| 94 | + OAuth2AuthorizedClientProvider authorizedClientProvider = |
| 95 | + OAuth2AuthorizedClientProviderBuilder.builder() |
| 96 | + .clientCredentials(clientCredentials -> |
| 97 | + clientCredentials.accessTokenResponseClient( |
| 98 | + createClientCredentialsTokenResponseClient(restClient))) |
| 99 | + .build(); |
| 100 | + // @formatter:on |
| 101 | + |
| 102 | + DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( |
| 103 | + clientRegistrationRepository, authorizedClientRepository); |
| 104 | + authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 105 | + |
| 106 | + OAuth2ClientHttpRequestInterceptor requestInterceptor = |
| 107 | + new OAuth2ClientHttpRequestInterceptor(authorizedClientManager); |
| 108 | + OAuth2AuthorizationFailureHandler authorizationFailureHandler = |
| 109 | + OAuth2ClientHttpRequestInterceptor.authorizationFailureHandler(authorizedClientRepository); |
| 110 | + requestInterceptor.setAuthorizationFailureHandler(authorizationFailureHandler); |
| 111 | + |
| 112 | + // @formatter:off |
| 113 | + return RestClient.builder() |
| 114 | + .requestFactory(clientHttpRequestFactory.get()) |
| 115 | + .requestInterceptor(requestInterceptor) |
| 116 | + .build(); |
| 117 | + // @formatter:on |
| 118 | + } |
| 119 | + |
| 120 | + @Bean |
| 121 | + public OAuth2AuthorizedClientManager authorizedClientManager( |
| 122 | + ClientRegistrationRepository clientRegistrationRepository, |
| 123 | + OAuth2AuthorizedClientRepository authorizedClientRepository, |
| 124 | + @Qualifier("default-client-http-request-factory") Supplier<ClientHttpRequestFactory> clientHttpRequestFactory) { |
| 125 | + |
| 126 | + RestClient restClient = accessTokenRestClient(clientHttpRequestFactory); |
| 127 | + |
| 128 | + // @formatter:off |
| 129 | + OAuth2AuthorizedClientProvider authorizedClientProvider = |
| 130 | + OAuth2AuthorizedClientProviderBuilder.builder() |
| 131 | + .authorizationCode() |
| 132 | + .refreshToken() |
| 133 | + .clientCredentials(clientCredentials -> |
| 134 | + clientCredentials.accessTokenResponseClient( |
| 135 | + createClientCredentialsTokenResponseClient(restClient))) |
| 136 | + .provider(new DeviceCodeOAuth2AuthorizedClientProvider()) |
| 137 | + .build(); |
| 138 | + // @formatter:on |
| 139 | + |
| 140 | + DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( |
| 141 | + clientRegistrationRepository, authorizedClientRepository); |
| 142 | + authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 143 | + |
| 144 | + // Set a contextAttributesMapper to obtain device_code from the request |
| 145 | + authorizedClientManager.setContextAttributesMapper(DeviceCodeOAuth2AuthorizedClientProvider |
| 146 | + .deviceCodeContextAttributesMapper()); |
| 147 | + |
| 148 | + return authorizedClientManager; |
| 149 | + } |
| 150 | + |
46 | 151 | @Bean("default-client-http-request-factory") |
47 | 152 | Supplier<ClientHttpRequestFactory> defaultClientHttpRequestFactory(SslBundles sslBundles) { |
48 | 153 | return () -> { |
@@ -82,4 +187,34 @@ Supplier<ClientHttpRequestFactory> selfSignedDemoClientHttpRequestFactory(SslBun |
82 | 187 | }; |
83 | 188 | } |
84 | 189 |
|
| 190 | + private static RestClient accessTokenRestClient(Supplier<ClientHttpRequestFactory> clientHttpRequestFactory) { |
| 191 | + // @formatter:off |
| 192 | + return RestClient.builder() |
| 193 | + .requestFactory(clientHttpRequestFactory.get()) |
| 194 | + .messageConverters((messageConverters) -> { |
| 195 | + messageConverters.clear(); |
| 196 | + messageConverters.add(new FormHttpMessageConverter()); |
| 197 | + messageConverters.add(new OAuth2AccessTokenResponseHttpMessageConverter()); |
| 198 | + }) |
| 199 | + .defaultStatusHandler(new OAuth2ErrorResponseErrorHandler()) |
| 200 | + .build(); |
| 201 | + // @formatter:on |
| 202 | + |
| 203 | + } |
| 204 | + |
| 205 | + private static OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> createClientCredentialsTokenResponseClient( |
| 206 | + RestClient restClient) { |
| 207 | + RestClientClientCredentialsTokenResponseClient clientCredentialsTokenResponseClient = |
| 208 | + new RestClientClientCredentialsTokenResponseClient(); |
| 209 | + clientCredentialsTokenResponseClient.addParametersConverter(authorizationGrantRequest -> { |
| 210 | + MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); |
| 211 | + // client_id parameter is required for tls_client_auth method |
| 212 | + parameters.add(OAuth2ParameterNames.CLIENT_ID, authorizationGrantRequest.getClientRegistration().getClientId()); |
| 213 | + return parameters; |
| 214 | + }); |
| 215 | + clientCredentialsTokenResponseClient.setRestClient(restClient); |
| 216 | + |
| 217 | + return clientCredentialsTokenResponseClient; |
| 218 | + } |
| 219 | + |
85 | 220 | } |
0 commit comments