Swagger UI空,并给出403

use*_*522 2 swagger-ui spring-boot swagger-2.0

我正在使用spring boot并且我已经添加了swagger到我的依赖项:

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.8.0</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

我的配置:

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我去这个网址时:

http:// localhost:8080/v2/api-docs它有效,我得到了json.

swagger ui http:// localhost:8080/swagger-ui.html

现在当我检查chrome中的网络选项卡时,我只是一个空页面,我看到了这个:

Failed to load resource: the server responded with a status of 403 ()
swagger-ui-standalone-preset.js Failed to load resource: the server responded with a status of 403 ()
swagger-ui.css Failed to load resource: the server responded with a status of 403 ()
springfox.js Failed to load resource: the server responded with a status of 403 ()
swagger-ui-bundle.js Failed to load resource: the server responded with a status of 403 ()
swagger-ui-standalone-preset.js Failed to load resource: the server responded with a status of 403 ()
springfox.js Failed to load resource: the server responded with a status of 403 ()
webjars/springfox-swagger-ui/favicon-32x32.png?v=2.8.0-SNAPSHOT Failed to load resource: the server responded with a status of 403 ()
webjars/springfox-swagger-ui/favicon-16x16.png?v=2.8.0-SNAPSHOT Failed to load resource: the server responded with a status of 403 ()
springfox.css Failed to load resource: the server responded with a status of 403 ()
swagger-ui.css Failed to load resource: the server responded with a status of 403 ()
Run Code Online (Sandbox Code Playgroud)

我正在使用spring boot security,我将其添加到我的安全配置中:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/v2/api-docs/**");
    web.ignoring().antMatchers("/swagger.json");
    web.ignoring().antMatchers("/swagger-ui.html");
}
Run Code Online (Sandbox Code Playgroud)

有人能帮助我吗?

Ind*_*sak 6

尝试在忽略列表中添加以下资源,

  • /swagger-resources/**
  • /webjars/**

这是完整的例子,

@Override
public void configure(WebSecurity web) throws Exception {    
    web.ignoring().antMatchers("/v2/api-docs/**");
    web.ignoring().antMatchers("/swagger.json");
    web.ignoring().antMatchers("/swagger-ui.html");
    web.ignoring().antMatchers("/swagger-resources/**");
    web.ignoring().antMatchers("/webjars/**");
}
Run Code Online (Sandbox Code Playgroud)


rie*_*pil 5

您必须在 Spring Security 配置中显式忽略所有必需的静态资源以进行 swagger。您从网络选项卡获得的错误消息表明浏览器能够加载swagger-ui.html文件但无法加载相关文件,.js/.css/images/icons因为它们在您的安全配置中没有被忽略。

试试这个解决方案:

@Configuration
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security", "/swagger-ui.html", "/webjars/**");
    }

}
Run Code Online (Sandbox Code Playgroud)

相关stackoverflow帖子:如何配置Spring Security以允许无需身份验证即可访问Swagger URL