2

I am using swagger, swagger ui with spring rest api to get a platform for testing/documenting the API, so I need to get oAuth2 authorisation working in swagger ui, I am using password grant with the authorisation server, so I had to use ResourceOwnerPasswordCredentialsGrant from the package springfox.documentation.servicewhich has a single parameter to its constructor, namely, the token url, I am setting that to the token endpoint in my authorisation server, but unfortunately, it does not persist token url and shows that as null in the authorisation window as follows:

null Authorisation URL in swagger ui

I could not find any example to use this particular type of grant with swagger ui, any help is much appreciated.

Hasson
  • 1,894
  • 1
  • 21
  • 25

1 Answers1

0

This is my configuration

public Docket oauth() {

    return new Docket(DocumentationType.SWAGGER_2).groupName("oauth")
            .securitySchemes(Arrays.asList(userOAuthScheme())).securityContexts(Arrays.asList(securityContext()))
            .select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
            .paths(not(ant("/admin/**")))
            .paths(not(ant("/admin.json")))
            .paths(not(ant("/error/**")))
            .paths(not(ant("/exception/**")))
            .paths(not(ant("/ping/**"))).build();
}

private OAuth userOAuthScheme() {
    List<AuthorizationScope> authorizationScopeList = new ArrayList<AuthorizationScope>();
    GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("http://localhost:8080/authServer/oauth/token");
    return new OAuth("oauth2", authorizationScopeList, Arrays.asList(grantType));
}

private SecurityContext securityContext() {
    return SecurityContext.builder().securityReferences(defaultAuth()).forPaths(PathSelectors.any()).build();
}

@Bean
public SecurityConfiguration securityInfo() {
    return new SecurityConfiguration("myClientId", "myClientSecret", "", "", "", ApiKeyVehicle.HEADER, "",
            " ");
}

private List<SecurityReference> defaultAuth() {
    final AuthorizationScope[] authorizationScopes = new AuthorizationScope[0];
    return Arrays.asList(new SecurityReference("oauth2", authorizationScopes));
}

On the Swagger screen take care in the "Setup client authentication" section

Type: Basic auth/ Request Body

It depends on your implementation, in my case works Basic auth.

I dont use scopes but you can add it on

              AuthorizationScope[] authorizationScopes
              List<AuthorizationScope> authorizationScopeList 
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Miguel Galindo
  • 111
  • 1
  • 3