How to Provide a SecurityConfiguration for each SecurityScheme in SwaggerUI












0















I am attempting to include multiple Security Schemes to be used by the authentication piece of Swagger UI. Each Security Scheme needs to have a different client-id/client-secret. I cannot figure out a way to provide multiple different client-id/client-secret, because the single SecurityConfiguration loads for all those fields.



I am aware as a security concern, it is not wise to include the client-id/client-secret. These values are only going to be available in our Dev and QA environments and only internal to our network.



@Autowired(required = false)
private ClientCredentialsResourceDetails clientCredentials;

@Bean
public SecurityConfiguration security() {
// Here we are loading the client-id/client-secret into the UI
// Apparently, this can only be configured for a single client-id/client-secret
SecurityConfigurationBuilder config = SecurityConfigurationBuilder.builder()
.clientId(clientCredentials.getClientId())
.clientSecret(clientCredentials.getClientSecret())
.scopeSeparator(" ")
.useBasicAuthenticationWithAccessCodeGrant(true);
return config.build();
}

@Bean
public Docket docket() {
ApiSelectorBuilder apiBuilder = new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.any());

for (String endpoint : swaggerProperties.getDocumentedEndpoints()) {
apiBuilder.paths(PathSelectors.regex(endpoint));
}
return apiBuilder.build()
.apiInfo(apiInfo())
// Setup different security schemes .securitySchemes(Arrays.asList(clientCredentialsSecurityScheme(), passwordSecurityScheme()))
.securityContexts(Arrays.asList(securityContext()));
}

private ApiInfo apiInfo() {
return new ApiInfo(swaggerProperties.getApiLongName(),
swaggerProperties.getApiDescription(),
swaggerProperties.getApiVersion(),
swaggerProperties.getTermsOfServiceUrl(),
new Contact("", "", swaggerProperties.getDeveloperEmail()),
swaggerProperties.getLicense(), swaggerProperties.getLicenseUrl(), Arrays.asList());
}

private String getAccessTokenUri() {
String accessTokenUri = null;
if(clientCredentials != null) {
accessTokenUri = clientCredentials.getAccessTokenUri();
}
return accessTokenUri;
}

private SecurityScheme clientCredentialsSecurityScheme() {
ClientCredentialsGrant grantType2 = new ClientCredentialsGrant(getAccessTokenUri());

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

private SecurityScheme passwordSecurityScheme() {
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(getAccessTokenUri());

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

private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(
Arrays.asList(new SecurityReference("client_credential_scheme", scopes()),
new SecurityReference("password_scheme", scopes())))
.forPaths(PathSelectors.regex("/v.*"))
.build();
}

private AuthorizationScope scopes() {
AuthorizationScope scopes = {};
return scopes;
}









share|improve this question



























    0















    I am attempting to include multiple Security Schemes to be used by the authentication piece of Swagger UI. Each Security Scheme needs to have a different client-id/client-secret. I cannot figure out a way to provide multiple different client-id/client-secret, because the single SecurityConfiguration loads for all those fields.



    I am aware as a security concern, it is not wise to include the client-id/client-secret. These values are only going to be available in our Dev and QA environments and only internal to our network.



    @Autowired(required = false)
    private ClientCredentialsResourceDetails clientCredentials;

    @Bean
    public SecurityConfiguration security() {
    // Here we are loading the client-id/client-secret into the UI
    // Apparently, this can only be configured for a single client-id/client-secret
    SecurityConfigurationBuilder config = SecurityConfigurationBuilder.builder()
    .clientId(clientCredentials.getClientId())
    .clientSecret(clientCredentials.getClientSecret())
    .scopeSeparator(" ")
    .useBasicAuthenticationWithAccessCodeGrant(true);
    return config.build();
    }

    @Bean
    public Docket docket() {
    ApiSelectorBuilder apiBuilder = new Docket(DocumentationType.SWAGGER_2).select()
    .apis(RequestHandlerSelectors.any());

    for (String endpoint : swaggerProperties.getDocumentedEndpoints()) {
    apiBuilder.paths(PathSelectors.regex(endpoint));
    }
    return apiBuilder.build()
    .apiInfo(apiInfo())
    // Setup different security schemes .securitySchemes(Arrays.asList(clientCredentialsSecurityScheme(), passwordSecurityScheme()))
    .securityContexts(Arrays.asList(securityContext()));
    }

    private ApiInfo apiInfo() {
    return new ApiInfo(swaggerProperties.getApiLongName(),
    swaggerProperties.getApiDescription(),
    swaggerProperties.getApiVersion(),
    swaggerProperties.getTermsOfServiceUrl(),
    new Contact("", "", swaggerProperties.getDeveloperEmail()),
    swaggerProperties.getLicense(), swaggerProperties.getLicenseUrl(), Arrays.asList());
    }

    private String getAccessTokenUri() {
    String accessTokenUri = null;
    if(clientCredentials != null) {
    accessTokenUri = clientCredentials.getAccessTokenUri();
    }
    return accessTokenUri;
    }

    private SecurityScheme clientCredentialsSecurityScheme() {
    ClientCredentialsGrant grantType2 = new ClientCredentialsGrant(getAccessTokenUri());

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

    private SecurityScheme passwordSecurityScheme() {
    GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(getAccessTokenUri());

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

    private SecurityContext securityContext() {
    return SecurityContext.builder()
    .securityReferences(
    Arrays.asList(new SecurityReference("client_credential_scheme", scopes()),
    new SecurityReference("password_scheme", scopes())))
    .forPaths(PathSelectors.regex("/v.*"))
    .build();
    }

    private AuthorizationScope scopes() {
    AuthorizationScope scopes = {};
    return scopes;
    }









    share|improve this question

























      0












      0








      0








      I am attempting to include multiple Security Schemes to be used by the authentication piece of Swagger UI. Each Security Scheme needs to have a different client-id/client-secret. I cannot figure out a way to provide multiple different client-id/client-secret, because the single SecurityConfiguration loads for all those fields.



      I am aware as a security concern, it is not wise to include the client-id/client-secret. These values are only going to be available in our Dev and QA environments and only internal to our network.



      @Autowired(required = false)
      private ClientCredentialsResourceDetails clientCredentials;

      @Bean
      public SecurityConfiguration security() {
      // Here we are loading the client-id/client-secret into the UI
      // Apparently, this can only be configured for a single client-id/client-secret
      SecurityConfigurationBuilder config = SecurityConfigurationBuilder.builder()
      .clientId(clientCredentials.getClientId())
      .clientSecret(clientCredentials.getClientSecret())
      .scopeSeparator(" ")
      .useBasicAuthenticationWithAccessCodeGrant(true);
      return config.build();
      }

      @Bean
      public Docket docket() {
      ApiSelectorBuilder apiBuilder = new Docket(DocumentationType.SWAGGER_2).select()
      .apis(RequestHandlerSelectors.any());

      for (String endpoint : swaggerProperties.getDocumentedEndpoints()) {
      apiBuilder.paths(PathSelectors.regex(endpoint));
      }
      return apiBuilder.build()
      .apiInfo(apiInfo())
      // Setup different security schemes .securitySchemes(Arrays.asList(clientCredentialsSecurityScheme(), passwordSecurityScheme()))
      .securityContexts(Arrays.asList(securityContext()));
      }

      private ApiInfo apiInfo() {
      return new ApiInfo(swaggerProperties.getApiLongName(),
      swaggerProperties.getApiDescription(),
      swaggerProperties.getApiVersion(),
      swaggerProperties.getTermsOfServiceUrl(),
      new Contact("", "", swaggerProperties.getDeveloperEmail()),
      swaggerProperties.getLicense(), swaggerProperties.getLicenseUrl(), Arrays.asList());
      }

      private String getAccessTokenUri() {
      String accessTokenUri = null;
      if(clientCredentials != null) {
      accessTokenUri = clientCredentials.getAccessTokenUri();
      }
      return accessTokenUri;
      }

      private SecurityScheme clientCredentialsSecurityScheme() {
      ClientCredentialsGrant grantType2 = new ClientCredentialsGrant(getAccessTokenUri());

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

      private SecurityScheme passwordSecurityScheme() {
      GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(getAccessTokenUri());

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

      private SecurityContext securityContext() {
      return SecurityContext.builder()
      .securityReferences(
      Arrays.asList(new SecurityReference("client_credential_scheme", scopes()),
      new SecurityReference("password_scheme", scopes())))
      .forPaths(PathSelectors.regex("/v.*"))
      .build();
      }

      private AuthorizationScope scopes() {
      AuthorizationScope scopes = {};
      return scopes;
      }









      share|improve this question














      I am attempting to include multiple Security Schemes to be used by the authentication piece of Swagger UI. Each Security Scheme needs to have a different client-id/client-secret. I cannot figure out a way to provide multiple different client-id/client-secret, because the single SecurityConfiguration loads for all those fields.



      I am aware as a security concern, it is not wise to include the client-id/client-secret. These values are only going to be available in our Dev and QA environments and only internal to our network.



      @Autowired(required = false)
      private ClientCredentialsResourceDetails clientCredentials;

      @Bean
      public SecurityConfiguration security() {
      // Here we are loading the client-id/client-secret into the UI
      // Apparently, this can only be configured for a single client-id/client-secret
      SecurityConfigurationBuilder config = SecurityConfigurationBuilder.builder()
      .clientId(clientCredentials.getClientId())
      .clientSecret(clientCredentials.getClientSecret())
      .scopeSeparator(" ")
      .useBasicAuthenticationWithAccessCodeGrant(true);
      return config.build();
      }

      @Bean
      public Docket docket() {
      ApiSelectorBuilder apiBuilder = new Docket(DocumentationType.SWAGGER_2).select()
      .apis(RequestHandlerSelectors.any());

      for (String endpoint : swaggerProperties.getDocumentedEndpoints()) {
      apiBuilder.paths(PathSelectors.regex(endpoint));
      }
      return apiBuilder.build()
      .apiInfo(apiInfo())
      // Setup different security schemes .securitySchemes(Arrays.asList(clientCredentialsSecurityScheme(), passwordSecurityScheme()))
      .securityContexts(Arrays.asList(securityContext()));
      }

      private ApiInfo apiInfo() {
      return new ApiInfo(swaggerProperties.getApiLongName(),
      swaggerProperties.getApiDescription(),
      swaggerProperties.getApiVersion(),
      swaggerProperties.getTermsOfServiceUrl(),
      new Contact("", "", swaggerProperties.getDeveloperEmail()),
      swaggerProperties.getLicense(), swaggerProperties.getLicenseUrl(), Arrays.asList());
      }

      private String getAccessTokenUri() {
      String accessTokenUri = null;
      if(clientCredentials != null) {
      accessTokenUri = clientCredentials.getAccessTokenUri();
      }
      return accessTokenUri;
      }

      private SecurityScheme clientCredentialsSecurityScheme() {
      ClientCredentialsGrant grantType2 = new ClientCredentialsGrant(getAccessTokenUri());

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

      private SecurityScheme passwordSecurityScheme() {
      GrantType grantType = new ResourceOwnerPasswordCredentialsGrant(getAccessTokenUri());

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

      private SecurityContext securityContext() {
      return SecurityContext.builder()
      .securityReferences(
      Arrays.asList(new SecurityReference("client_credential_scheme", scopes()),
      new SecurityReference("password_scheme", scopes())))
      .forPaths(PathSelectors.regex("/v.*"))
      .build();
      }

      private AuthorizationScope scopes() {
      AuthorizationScope scopes = {};
      return scopes;
      }






      spring swagger-ui swagger-2.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 20:32









      DashingSuprHeroDashingSuprHero

      5226




      5226
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382209%2fhow-to-provide-a-securityconfiguration-for-each-securityscheme-in-swaggerui%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382209%2fhow-to-provide-a-securityconfiguration-for-each-securityscheme-in-swaggerui%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()