question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring Boot Swagger 2 UI Oauth2 configuration

See original GitHub issue

I 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": {

    }
}

image image

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:20 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
dilipkrishcommented, Aug 1, 2016

@trvajjala This issue has been fixed in 2.5.0, Please create a new issue if it is still a problem

0reactions
tvajjalacommented, Aug 1, 2016

facing same issue , did you get any solution yet

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found