启用 swagger2 时无法访问浏览器上的 UI

问题描述 投票:0回答:0

pom.xml
包含:

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.1</version>
   </dependency>

   <dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.1</version>
</dependency>

招摇配置文件:

@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {

    @RequestMapping(value = "/docs", method = RequestMethod.GET)
    public String docs() {
        return "redirect:/swagger-ui.html";
    }

    @Bean
    public Docket apiDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .securitySchemes(Arrays.asList(apiKey()))
            .securityContexts(Arrays.asList(securityContext()))
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
     return new ApiInfoBuilder()
         .title("APIReference")
         .version("1.0.0")
         .description("something")
         .build();
    }

    @Bean
    SecurityContext securityContext() {
        return SecurityContext.builder()
            .securityReferences(defaultAuth())
            .forPaths(PathSelectors.any())
            .build();
    }

    List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("JWT", authorizationScopes));
    }

    private ApiKey apiKey() {
        return new ApiKey("JWT", "Authorization", "header");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

        registry
           .addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

Application.properties
文件中:

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
springfox.documentation.swagger.v2.path=/swagger.json

为了绕过 Swagger 的 Spring Security,我在

WebSecurityConfigure
文件中添加了以下内容:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/resources/**", "/static/**");
    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/**");
    web.ignoring().antMatchers("/configuration/ui", "/swagger-resources", "/configuration/security");
}

当我尝试从浏览器访问 API 时,我得到以下信息:

enter image description here

我在邮递员或失眠中尝试时得到这个:

enter image description here

浏览器中的网页是这样的:

enter image description here

即使在 insomnia/Postman(200 OK)中,UI 也无法正确呈现,并且浏览器给我错误。

JSON 和 HTML 在失眠中变得很好。 我试图从 MS Edge 和 chrome 中访问 URL。

有人可以帮我解决这个问题吗?

spring-boot google-chrome spring-security swagger-ui swagger-2.0
© www.soinside.com 2019 - 2024. All rights reserved.