Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access to springfox swagger-ui behind spring-security

I am going to use springfox (2.6.1v) with swagger-ui in my Spring Boot (1.4.2v).

The configuration for it looks:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
               .select()
               .apis(RequestHandlerSelectors.any())
               .paths(PathSelectors.any())
               .build()
               .genericModelSubstitutes(ResponseEntity.class);
  }
}

The problem is that my swagger is behind spring security and I need to allow access there only by admin.

Question is what should be the set of matchers to allow swagger-ui to work within my application?

public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("??? <<<what_should_be_here>>> ???") // what should be here?
        .hasRole("TENANT_ADMIN");
  }
}
like image 994
Ziemowit Stolarczyk Avatar asked Dec 16 '16 07:12

Ziemowit Stolarczyk


People also ask

How do I authorize Swagger UI?

In the Swagger Editor (the right pane), click the Authorize button, paste the sample API key shown in the description into the Value field (or use your own OpenWeatherMap API key), and click Authorize. Then click Close to close the authorization modal.

What is the URL for Swagger UI in spring boot?

You need to add one additional dependency to the pom. xml to use Swagger UI. Now you can get the Swagger UI using http://localhost:8080/swagger-ui.html#/. Here you can expand those controllers and see all the endpoints it has.

How do I get Swagger UI in HTML?

Generating HTML documentation using Swagger-ui.Add Swagger-ui dependency - (https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui). This will provide an endpoint at the root url, which provides the Swagger HTML documentation. This will be located at localhost:8080/swagger-ui.


1 Answers

Ok first I have found the solution here so following lines:

.antMatchers("/admin/system/state/*", "/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**")

But still sth did not work and because of that I have asked this question. But after deeper investigation it occured that spring-fox does not support GSON. When you use GSON as "to json" converter swagger-ui receives a slightly different JSON format what causes problems...

When we changed converter to Jackson and added above paths to spring-config it works without any problems.

I have even requested the new feature on spring-fox github here.

like image 127
Ziemowit Stolarczyk Avatar answered Sep 23 '22 21:09

Ziemowit Stolarczyk