java Spring Boot with Swagger - 无法加载远程配置

Ric*_*ard 7 java swagger spring-boot

我有一个 Java 11 Spring Boot 2.5.1 应用程序。

Swagger 通过 http 在本地工作:

我添加了 Swagger,它在我的本地主机上完美运行:

http://localhost:8085/swagger-ui/index.html#/

在此输入图像描述

Swagger 无法通过 https 远程工作:

但是,当我将其部署到远程服务器时,出现以下错误:

https://mycompany.co/pow-wow/swagger-ui/index.html#/

在此输入图像描述

错误

无法加载远程配置。

注意:远程服务器是通过https访问的。

REST 端点可在远程服务器上访问。

例如 GET https://mycompany.co/pow-wow/powwow/test-me按预期返回 200。

问题

如何通过远程服务器访问 Swagger?

代码

pom.xml

    <!-- Swagger -->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.7</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

WebSecurityConfig.java

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //http.csrf().disable().authorizeRequests().antMatchers("/powwow/*").hasRole("USER").and().httpBasic();
        //http.csrf().disable().authorizeRequests().antMatchers("/*").permitAll().and().httpBasic();
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests().antMatchers("/soapWS/**").permitAll().and()
                .authorizeRequests().antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll().and()
                .authorizeRequests().antMatchers("/actuator/**").permitAll()
                .anyRequest().authenticated().and()
                .httpBasic().and()
                .csrf().disable();
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息:

当访问Swagger时查看浏览器中的网络流量。

在本地,有一个电话:

http://localhost:8085/v3/api-docs/swagger-config

返回:

在此输入图像描述

{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8085/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}
Run Code Online (Sandbox Code Playgroud)

远程调用:

https://mycompany.co/v3/api-docs/swagger-config

它返回一个302.

在此输入图像描述

但是https://mycompany.co/pow-wow/v3/api-docs/swagger-config

返回:

{"configUrl":"/v3/api-docs/swagger-config","oauth2RedirectUrl":"http://localhost:8085/swagger-ui/oauth2-redirect.html","url":"/v3/api-docs","validatorUrl":""}
Run Code Online (Sandbox Code Playgroud)

因此,这表明问题与/pow-wow调用中缺少上下文路径有关。

GJo*_*nes 8

你应该尝试添加

springdoc:
  swagger-ui:
    config-url: /pow-wow/v3/api-docs/swagger-config
    url: /v3/api-docs
Run Code Online (Sandbox Code Playgroud)

到您的 application.properties。在您提供的示例中

https://mycompany.co/pow-wow/v3/api-docs/swagger-config
Run Code Online (Sandbox Code Playgroud)

它表明您的路径中有“/pow-wow/”。Swagger 需要知道从哪里获取其配置。