|
1 | | -# OpenAPI generated API stub |
2 | | - |
3 | | -[Spring Framework 6 HTTP Interface](https://docs.spring.io/spring-framework/docs/6.0.0/reference/html/integration.html#rest-http-interface) |
| 1 | +# Spring HTTP Interface with OAuth2 (@ClientRegistrationId) |
4 | 2 |
|
| 3 | +This sample demonstrates the use of the `@ClientRegistrationId` annotation with Spring HTTP Interface clients. |
5 | 4 |
|
6 | 5 | ## Overview |
| 6 | + |
7 | 7 | This code was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. |
8 | | -By using the [OpenAPI-Spec](https://openapis.org), you can easily generate an API stub. |
9 | | -This is an example of building API stub interfaces in Java using the Spring framework. |
| 8 | +When generating Spring HTTP Interface clients, you can specify a `clientRegistrationId` parameter to automatically add the `@ClientRegistrationId` annotation to all generated API interfaces. |
| 9 | + |
| 10 | +## Configuration |
| 11 | + |
| 12 | +Add the `clientRegistrationId` property to your generator configuration: |
| 13 | + |
| 14 | +```yaml |
| 15 | +generatorName: spring |
| 16 | +library: spring-http-interface |
| 17 | +additionalProperties: |
| 18 | + clientRegistrationId: "petstore-oauth" |
| 19 | +``` |
| 20 | +
|
| 21 | +Or via command line: |
| 22 | +
|
| 23 | +```bash |
| 24 | +openapi-generator-cli generate \ |
| 25 | + -g spring \ |
| 26 | + --library spring-http-interface \ |
| 27 | + --additional-properties clientRegistrationId=petstore-oauth \ |
| 28 | + -i petstore.yaml \ |
| 29 | + -o ./output |
| 30 | +``` |
| 31 | + |
| 32 | +## Generated Code |
| 33 | + |
| 34 | +The generated interface will include the `@ClientRegistrationId` annotation at the class level: |
| 35 | + |
| 36 | +```java |
| 37 | +@ClientRegistrationId("petstore-oauth") |
| 38 | +public interface PetApi { |
| 39 | + |
| 40 | + @HttpExchange( |
| 41 | + method = "GET", |
| 42 | + value = "/pet/{petId}", |
| 43 | + accept = { "application/json" } |
| 44 | + ) |
| 45 | + ResponseEntity<PetDto> getPetById(@PathVariable("petId") Long petId); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Spring Security Integration |
| 50 | + |
| 51 | +This annotation is part of Spring Security's OAuth2 integration for HTTP Service Clients. It automatically associates OAuth2 tokens with HTTP requests. |
| 52 | + |
| 53 | +### Requirements |
| 54 | + |
| 55 | +- Spring Boot 3.5+ |
| 56 | +- Spring Security 6.5+ |
| 57 | + |
| 58 | +### Application Properties |
| 59 | + |
| 60 | +Configure your Spring application with the OAuth2 client registration: |
| 61 | + |
| 62 | +```yaml |
| 63 | +spring: |
| 64 | + security: |
| 65 | + oauth2: |
| 66 | + client: |
| 67 | + registration: |
| 68 | + petstore-oauth: |
| 69 | + client-id: your-client-id |
| 70 | + client-secret: your-client-secret |
| 71 | + authorization-grant-type: client_credentials |
| 72 | + scope: read,write |
| 73 | + provider: |
| 74 | + petstore-oauth: |
| 75 | + token-uri: https://auth.example.com/oauth/token |
| 76 | +``` |
| 77 | +
|
| 78 | +### Bean Configuration |
| 79 | +
|
| 80 | +Configure your HTTP Interface beans with OAuth2 support using `RestClient` and the `OAuth2ClientHttpRequestInterceptor`: |
10 | 81 |
|
11 | | -The stubs generated can be used in your existing Spring application for HTTP integration with other REST services |
12 | | -To use auto-generated interfaces you have to create your own configuration which extends default abstract configurator & provide `WebClient` instance via constructor |
13 | 82 | ```java |
14 | 83 | @Configuration |
15 | | -public class MyConfiguration extends org.openapitools.configuration.HttpInterfacesAbstractConfigurator { |
| 84 | +public class HttpInterfaceConfig { |
| 85 | +
|
| 86 | + @Bean |
| 87 | + public PetApi petApi(RestClient.Builder builder, OAuth2AuthorizedClientManager authorizedClientManager) { |
| 88 | + OAuth2ClientHttpRequestInterceptor interceptor = |
| 89 | + new OAuth2ClientHttpRequestInterceptor(authorizedClientManager); |
| 90 | +
|
| 91 | + RestClient restClient = builder |
| 92 | + .baseUrl("https://petstore.example.com/v2") |
| 93 | + .requestInterceptor(interceptor) |
| 94 | + .build(); |
16 | 95 |
|
17 | | - public MyConfiguration(WebClient myWebClient) { // separately created WebClient instance |
18 | | - super(myWebClient); |
| 96 | + HttpServiceProxyFactory factory = HttpServiceProxyFactory |
| 97 | + .builderFor(RestClientAdapter.create(restClient)) |
| 98 | + .build(); |
| 99 | +
|
| 100 | + return factory.createClient(PetApi.class); |
19 | 101 | } |
20 | 102 | } |
21 | | -``` |
| 103 | +``` |
| 104 | + |
| 105 | +## References |
| 106 | + |
| 107 | +- [Spring Security HTTP Service Client Integration](https://docs.spring.io/spring-security/reference/servlet/oauth2/client/http-service-client.html) |
| 108 | +- [Spring Framework HTTP Interface](https://docs.spring.io/spring-framework/reference/integration/rest-clients.html#rest-http-interface) |
0 commit comments