【问题标题】:Swagger Doc not working after adding ssl to domain将 ssl 添加到域后,Swagger Doc 无法正常工作
【发布时间】:2021-12-04 08:18:35
【问题描述】:

我的 Spring Boot 应用程序中的 swagger 文档运行良好。但是,在我将https 添加到我的域后,它突然停止工作。所有端点不断返回TypeError: Failed To Fetch

下面是我的招摇配置

public class SwaggerConfig {

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

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

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

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

Spring Security Config 让 swagger 可以访问

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

请问有人知道如何解决这个问题

【问题讨论】:

    标签: spring-boot swagger


    【解决方案1】:

    问题不在于招摇配置。问题是由于错误的 nginx 配置造成的。在 location 块中,我在 proxy_pass 之后添加了以下内容

    proxy_pass http://localhost:8080/;
    // Added
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-01
      • 2016-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      相关资源
      最近更新 更多