Skip to content

Commit f03aa28

Browse files
ng-galienclaude
andcommitted
docs: Add OAuth2 configuration example to README
- Add README.md to .openapi-generator-ignore to preserve custom docs - Include proper OAuth2ClientHttpRequestInterceptor configuration example - Document Spring Boot 3.5+ / Spring Security 6.5+ requirements Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent d8187a4 commit f03aa28

2 files changed

Lines changed: 101 additions & 11 deletions

File tree

samples/client/petstore/spring-http-interface-oauth/.openapi-generator-ignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@
2121
#docs/*.md
2222
# Then explicitly reverse the ignore rule for a single file:
2323
#!docs/README.md
24+
25+
# Preserve custom OAuth2 documentation
26+
README.md
Lines changed: 98 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,108 @@
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)
42

3+
This sample demonstrates the use of the `@ClientRegistrationId` annotation with Spring HTTP Interface clients.
54

65
## Overview
6+
77
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`:
1081

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
1382
```java
1483
@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();
1695
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);
19101
}
20102
}
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

Comments
 (0)