Forum Discussion

ddronavalli's avatar
ddronavalli
New Member
5 years ago

I have a rest api that in turn calls a rest service secured by Oauth 2.0 - enable swagger ui

Hello everyone,

 

I have a rest api that in turn calls a rest service secured by Oauth 2.0. If I enable oauth for swagger, its treating my service as secured service but failing to authenticate the actual oauth rest api that is being called from my service. Does anyone have an idea on how to enable swagger so that my rest service can call other authenticated rest api. Below is my current swagger config. I am able to succesfully authorize the credentials on swagger ui

 

@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.securitySchemes(Arrays.asList(securityScheme()))
.securityContexts(Arrays.asList(securityContext()));
}


@Bean public SecurityConfiguration security() { return
SecurityConfigurationBuilder.builder() .clientId(
"id")
.clientSecret("secret") .scopeSeparator(" ")
.useBasicAuthenticationWithAccessCodeGrant(true) .build(); }

public SecurityScheme securityScheme() { GrantType grantType = new
AuthorizationCodeGrantBuilder().tokenEndpoint( new TokenEndpoint("https://www.googleapis.com/oauth2/v4"
+ "/token", "oauthtoken")) .tokenRequestEndpoint( new
TokenRequestEndpoint("https://accounts.google.com/o/oauth2/v2" + "/auth", "id", "secret"))
.build();

SecurityScheme oauth = new OAuthBuilder().name("spring_oauth")
.grantTypes(Arrays.asList(grantType))
.scopes(Arrays.asList(scopes()))
.build(); return oauth; }

public AuthorizationScope[] scopes() {
AuthorizationScope[] scopes = {
new AuthorizationScope("https://www.googleapis.com/auth/bigquery", "for bigquery operations")};
return scopes; }

private SecurityContext securityContext() { return
SecurityContext.builder().securityReferences(Arrays.asList(new
SecurityReference("spring_oauth",
(springfox.documentation.service.AuthorizationScope[]) scopes())))
.forPaths(PathSelectors.regex("/gcpconnector.*")) .build(); }


}

No RepliesBe the first to reply