Spring Boot Swagger 2 UI Oauth2 configuration
See original GitHub issueI have some troubles getting Swagger UI running for my OAuth2 secured API with the current 2.0.0 release.
I guess I’m missing something…
I used following Configuration
@Bean
public Docket apiDocumentation() {
return new Docket(DocumentationType.SWAGGER_2).groupName("api").apiInfo(apiInfo())
.select().paths(internalPaths()).build()
.securitySchemes(newArrayList(securitySchema()))
.securityContexts(newArrayList(securityContext()));
}
public static final String securitySchemaOAuth2 = "oauth2schema";
public static final String authorizationScopeGlobal = "global";
public static final String authorizationScopeGlobalDesc ="accessEverything";
private OAuth securitySchema() {
AuthorizationScope authorizationScope = new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobal);
LoginEndpoint loginEndpoint = new LoginEndpoint("http://localhost:9999/sso/login");
GrantType grantType = new ImplicitGrant(loginEndpoint, "access_token");
return new OAuth(securitySchemaOAuth2, newArrayList(authorizationScope), newArrayList(grantType));
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(internalPaths())
.build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope(authorizationScopeGlobal, authorizationScopeGlobalDesc);
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return newArrayList(
new SecurityReference(securitySchemaOAuth2, authorizationScopes));
}
My API methods are annotated like that
@RequestMapping(value = "/languageFile", method = RequestMethod.GET)
@ApiOperation(value = "Fetches the language file of a given or the default locale",
authorizations = {@Authorization(value = SwaggerConfig.securitySchemaOAuth2, type = "oauth2", scopes =
{@AuthorizationScope( scope = SwaggerConfig.authorizationScopeGlobal, description = SwaggerConfig.authorizationScopeGlobalDesc)})})
public I18NGetLanguageFileResponse getLanguageFile(@RequestParam(defaultValue = TechnicalConfig.defaultLocaleString, value = "locale", required = false) String localeString) {
The Result
The problem now is that the authentication button appears in Swagger UI but isn’t invokable. There isn’t even any js attachted to this button.
{
"swagger": "2.0",
"info": {
},
"host": "localhost:9000",
"basePath": "/rest",
"tags": [
{
"name": "i18n-controller"
}
],
"paths": {
"/i18n/languageFile": {
"get": {
"tags": [
"i18n-controller"
],
"summary": "Fetches the language file of a given or the default locale",
"description": "getLanguageFile",
"operationId": "getLanguageFileUsingGET",
"consumes": [
"application/json"
],
"produces": [
"*/*"
],
"parameters": [
{
"name": "locale",
"in": "query",
"description": "localeString",
"required": false,
"type": "string"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/I18NGetLanguageFileResponse"
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"security": [
{
"oauth2schema": [
"global"
]
}
]
}
}
},
"securityDefinitions": {
"oauth2schema": {
"type": "oauth2",
"authorizationUrl": "http://localhost:9999/sso/login",
"flow": "implicit",
"scopes": {
"global": "global"
}
}
},
"definitions": {
}
}
Issue Analytics
- State:
- Created 8 years ago
- Comments:20 (6 by maintainers)
Top Results From Across the Web
Setting Up Swagger 2 with a Spring REST API - Baeldung
In this tutorial, we'll look at Swagger 2 for a Spring REST web service, using the Springfox implementation of the Swagger 2 specification....
Read more >How to configure oAuth2 with password flow with Swagger ui ...
1) Swagger Config: package com.example.api; import org. · 2) in POM use this Swagger UI version 2.7.0: <dependency> <groupId>io. · 3) in the...
Read more >rrohitramsen/spring-boot-oauth2-jwt-swagger-ui - GitHub
Use above given user details to login and generate the authorization token. Swagger-Home. Login using the generated token Swagger-Home. Change OAuth ...
Read more >OAuth2 protecting Spring Boot Microservices with Swagger
Then I need to update the Swagger Configuration to tell it to use OAuth2 when using the micro service. ... In the above...
Read more >Swagger with Spring Boot and Security | by Necmeddin Tapan
Swagger -ui can be used with above configuration, but if REST API is not secured. As known, using web services as insecure is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@trvajjala This issue has been fixed in 2.5.0, Please create a new issue if it is still a problem
facing same issue , did you get any solution yet