Forum Discussion

munozy's avatar
munozy
New Member
4 years ago

How to set the clientId and the clientSecret for displaying automatically on Authorization page?

On my Spring Boot application, I am trying to replace Swagger 2 with OpenApi 3. In the current implementation of SwaggerConfiguration class,

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

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

private SecurityScheme securityScheme() {
    GrantType grantType = new AuthorizationCodeGrantBuilder()
            .tokenEndpoint(new TokenEndpoint(tokenEndpoint, "code"))
            .tokenRequestEndpoint(new TokenRequestEndpoint(tokenRequestEndpoint,
                    swaggerCredentialsProvider.getClientId(),
                    swaggerCredentialsProvider.getClientSecret()))
            .build();

    return new OAuthBuilder().name("spring_oauth")
            .grantTypes(Collections.singletonList(grantType))
            .scopes(Arrays.asList(scopes())).build();
}
...
}

In this sample code, I give the clientId and the clientSecret and it will display automatically on the swagger-ui Authorization page:

 

In my new implementation of OpenApi Configuration

@Bean
public OpenAPI customOpenAPI() {
    OAuthFlow oAuthFlow = new OAuthFlow()
            .tokenUrl(tokenEndpoint)
            .authorizationUrl(tokenRequestEndpoint)
            .scopes(new Scopes().addString(scope, ""));
    return new OpenAPI()
            .components(new Components()
                    .addSecuritySchemes("security_auth", new SecurityScheme()
                            .flows(new OAuthFlows().authorizationCode(oAuthFlow))
                            .type(SecurityScheme.Type.OAUTH2).scheme("oauth2")))
            .info(new Info()
                    .title(appName)
                    .version(appVersion)
                    .description(appDescription));
}

I do not find a way to set theses information. I tried to set springdoc.swagger-ui.oauth.clientId in the application.property file, but the clientId did not display.

How to set the clientId and the clientSecret with OpenApi 3 for displaying automatically on the Authorization Page?

No RepliesBe the first to reply