Springboot:无法访问Springfox apidocs

时间:2018-08-16 09:56:23

标签: java spring-boot spring-oauth2 springfox

我添加了oauth2安全性:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>

另一方面,我添加了springfox swagger:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>${springfox-swagger.version}</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>${springfox-swagger.version}</version>
</dependency>

我已经配置了资源服务器,以便能够请求api文档:

@Override
public void configure(HttpSecurity http) throws Exception {
    http.requestMatchers()
        .antMatchers(SECURED_PATTERN).and().authorizeRequests()
        .antMatchers("/actuator/**").permitAll()
        .antMatchers("/api/**").permitAll()
        .antMatchers("/swagger-ui.html").permitAll()
        .antMatchers("/webjars/**").permitAll()
        .antMatchers(HttpMethod.OPTIONS).permitAll()
        .antMatchers(HttpMethod.POST, SECURED_PATTERN).access(SECURED_WRITE_SCOPE)
        .anyRequest().access(SECURED_READ_SCOPE);
}

因此,我的SwaggerConfig类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage(DocumentController.class.getPackage().getName()))
          .paths(PathSelectors.any())
          .build()
          .apiInfo(metaData());
    }

    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("...")
                //...
                .build();
    }
}

但是,当我尝试访问api/v2/api-docs时,却遇到了404错误。

有什么想法吗?

0 个答案:

没有答案