-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Swagger OAUTH Proxy to avoid CORS issue #1
base: main
Are you sure you want to change the base?
Conversation
There are further issues opened in the past pertaining to this: |
return response.getBody(); | ||
|
||
} else { | ||
throw new RuntimeException("Authorization header missing or not using Basic Auth"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there maybe from Spring a more specific Exception we can use to indicate 401 Unauthorized because Basic Auth header is missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have changed it to a ResponseStatusException
public class SwaggerOauthProxyController { | ||
|
||
private final String GRANT_TYPE_KEY = "grant_type"; | ||
private final String CLIENT_CREDENTIALS_KEY = "client_credentials"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is the value in a Map entry, let's maybe call it CLIENT_CREDENTIALS_VALUE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, much better, thanks!
Maybe the following paragraph can be added under section 1. of the PR's README: 1. Add the configuration shown in the Summary section in your application properties In case your authentication provider adheres to OpenID Connect and you have defined the Spring Security OAuth2 client properties using issuer URI, then you could reference the token URI via the issuer URI:
|
Greetings,
While integrating Swagger with my application, I encountered issue #5104 related to authentication in the Swagger UI. To address this, I implemented a workaround that enables the application to act as a proxy between the OAuth provider (Keycloak, in this case) and the Swagger UI.
This pull request generalizes my solution into a configuration option for the springdoc-openapi dependency, enabling easier adoption in similar use cases.
Summary of Changes
This pull request introduces the following configuration options for enabling and managing an OAuth proxy in Swagger UI. Here we have some example values:
How to Use
To enable this feature in your application, follow these steps:
1. Add the configuration shown in the Summary section in your application properties
In case your authentication provider adheres to OpenID Connect and you have defined the Spring Security OAuth2 client properties using issuer URI, then you could reference the token URI via the issuer URI:
springdoc.swagger-ui.oauth-proxy.oauth-token-uri=${spring.security.oauth2.client.provider.my-oauth-server.issuer-uri}/protocol/openid-connect/token
2. Add an OpenAPI Configuration Class
Create a configuration class with the following annotations and setup:
Mainly this is an example, the most important parts to take into account are inside the
@SecurityScheme
:and inside the
@OAuthFlow
referencing the proxy path:3. Configure the Security Filter Chain
If the user has a SecurityConfig class, it might look like this:
To use this proxy the user needs to
@Autowired
the OauthProxy properties:And add add the proxy path to
SecurityFilterChain
to allow POST requests:Additional notes
Please let me know if further details or changes are required. Sorry for the clumsiness, and thank you for considering this contribution! It is actually my first one.