Springfox swagger-ui.html无法推断基本网址-由缺少Cookie引起

Arn*_*pta 12 spring swagger swagger-ui spring-boot springfox

我们在API网关后面提供了Spring Boot服务。使用较早版本的Springfox-2.1.2,我们在加载swagger-ui.html页面时没有任何问题。这与Spring Boot 1.4.3.RELEASE一起使用。从那时起,我们已升级到Boot 1.5.7,并将Springfox升级到了2.8.0。

现在,如果我们加载页面,则会显示一个带有以下长消息的警报框。

无法推断基本网址。当使用动态servlet注册或API位于API网关后面时,这很常见。基本url是为所有swagger资源提供服务的根。例如,如果api在http://example.org/api/v2/api-docs上可用,则基本URL是http://example.org/api/。请手动输入位置

我在网上搜索时得到了一些提示,但是这些情况似乎不适用于我们。例如,如果我只是还原版本,它将通过相同的API网关再次开始工作。

跟踪流量,似乎调用了.html页所提供的三个XHR资源引起了问题。这些从我们的API网关返回401。它们返回401的原因是未传递cookie。

这三个调用是:

如果我将这些URL作为纯浏览器请求加载-它们可以工作-因为发送了cookie。

我怀疑是否适用CORS,因为HTML是从与招摇的JSON和实际服务调用相同的地址提供的。

知道为什么会这样吗?有人遇到类似的问题吗?解决方法的建议?在此先感谢。

小智 9

添加安全配置-跳过以下用于身份验证的URL ::

private static final String[] AUTH_WHITELIST = {
        "/swagger-resources/**",
        "/swagger-ui.html",
        "/v2/api-docs",
        "/webjars/**"
};

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(AUTH_WHITELIST);
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*los 8

请参阅下面的编辑

你使用弹簧安全吗?

如果是,您可能会跳过一些这样的资源(对吗?): "/swagger-resources/**", "/swagger-ui.html", "/v2/api-docs", "/webjars/**"

尝试将其更改"/swagger-resources/**""**/swagger-resources/**".

我对 swagger 的特定安全配置是:

private static final String[] AUTH_LIST = {
        // -- swagger ui
        "**/swagger-resources/**",
        "/swagger-ui.html",
        "/v2/api-docs",
        "/webjars/**"
};

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests().antMatchers(AUTH_LIST).authenticated()
    .and()
    .httpBasic().authenticationEntryPoint(swaggerAuthenticationEntryPoint())
    .and()
    .csrf().disable();
}

@Bean
public BasicAuthenticationEntryPoint swaggerAuthenticationEntryPoint() {
    BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
    entryPoint.setRealmName("Swagger Realm");
    return entryPoint;
}
Run Code Online (Sandbox Code Playgroud)

如果您需要/想要,我可以将示例项目发送到 GitHub,让您了解有关我的安全性/swagger 配置的更多信息。

编辑 2018/04/10

这个问题是springfox版本错误导致的。在github上看到这个问题来解决问题

给后人:

在 pom.xml 中

...
<repositories>
    <repository>
        <id>swagger</id>
        <name>swagger</name>
        <url>http://oss.jfrog.org/artifactory/oss-snapshot-local</url>
    </repository>
</repositories>
...
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.1-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.1-SNAPSHOT</version>
</dependency>
...
Run Code Online (Sandbox Code Playgroud)

扩展 WebSecurityConfigAdapter 的类:

@Configuration
public class WebSecurityConfigEntryPointApplication extends WebSecurityConfigurerAdapter {

    private static final List<String> AUTH_LIST = Arrays.asList(
            "/swagger-resources/**",
            "/swagger-ui.html**",
            "/webjars/**",
            "favicon.ico");

    @Autowired
    private RestAuthenticationEntryPoint restAuthenticationEntryPoint;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .antMatcher("/**").authorizeRequests().anyRequest().authenticated()
                .and()
                .exceptionHandling()
                .defaultAuthenticationEntryPointFor(swaggerAuthenticationEntryPoint(), new CustomRequestMatcher(AUTH_LIST))
                .and()
                .httpBasic()
                .authenticationEntryPoint(restAuthenticationEntryPoint)
                .and()
                .csrf().disable();
    }

    @Bean
    public BasicAuthenticationEntryPoint swaggerAuthenticationEntryPoint() {
        BasicAuthenticationEntryPoint entryPoint = new BasicAuthenticationEntryPoint();
        entryPoint.setRealmName("Swagger Realm");
        return entryPoint;
    }

    private class CustomRequestMatcher implements RequestMatcher {

        private List<AntPathRequestMatcher> matchers;

        private CustomRequestMatcher(List<String> matchers) {
            this.matchers = matchers.stream().map(AntPathRequestMatcher::new).collect(Collectors.toList());
        }

        @Override
        public boolean matches(HttpServletRequest request) {
            return matchers.stream().anyMatch(a -> a.matches(request));
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

RestAuthenticationEntryPoint:

@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
    }
}
Run Code Online (Sandbox Code Playgroud)


Ari*_*dam 7

在spring boot类中添加以下注释对我来说解决了这个问题。

@ EnableSwagger2

我正在使用摇摇欲坠的版本

 <version>2.9.2</version>
Run Code Online (Sandbox Code Playgroud)


car*_*ira 5

这发生在我身上,我使用的是 SpringBoot 1.5.16 和 Springfox 2.9.1。

在我的 中application.properties,我已经定义了server.servlet-path=/api,但是,不知何故,swagger-ui 忽略了定义的值。我尝试了很多不同的方法来完成这项工作,最后我找到了一个解决方法:

 @Configuration
 @EnableSwagger2
 public class SwaggerConfiguration extends WebMvcConfigurationSupport {                                    

    @Bean
    public Docket apiMonitoramento() { 
        return new Docket(DocumentationType.SWAGGER_2)
                .select()                                  
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())                          
                .build()    
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()              
                .title("REST API")
                .description("Servicesx")               
                .build();
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
 }
Run Code Online (Sandbox Code Playgroud)

我正在访问http://localhost:8080/context/swagger-ui.html,但使用该配置,正确的 URL 是:http://localhost:8080/context/api/swagger-ui.html