在 spring-security 后面访问 springfox swagger-ui

Get access to springfox swagger-ui behind spring-security

我将在我的 Spring 引导程序 (1.4.2v) 中使用带有 swagger-ui 的 springfox (2.6.1v)。

它的配置看起来:

@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);
  }
}

问题是我的狂妄是 spring 安全的背后,我需要只允许管理员访问那里。

问题是,允许 swagger-ui 在我的应用程序中工作的匹配器集应该是什么?

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");
  }
}

好的,首先我找到了解决方案 所以下面几行:

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

但是还是没有用,因此我问了这个问题。但经过深入调查后发现 spring-fox 不支持 GSON。当您将 GSON 用作 "to json" 转换器时,swagger-ui 收到略有不同的 JSON 格式,导致问题的原因...

当我们将转换器更改为 Jackson 并将以上路径添加到 spring-config 时,它可以正常工作。

我什至在 spring-fox github here.

上请求了新功能