Springfox swagger-ui.html unable to infer base URL - Caused by missing cookies
我们在API网关后面提供了Spring Boot服务。使用Springfox的早期版本-2.1.2,加载
现在,如果我们加载页面,则会显示一个带有以下长消息的警报框。
Unable to infer base url. This is common when using dynamic servlet
registration or when the API is behind an API Gateway. The base url is
the root of where all the swagger resources are served. For e.g. if
the api is available at http://example.org/api/v2/api-docs then the
base url is http://example.org/api/. Please enter the location
manually
我在网上搜索时得到了一些提示,但是这些情况似乎不适用于我们。例如,如果我只是还原版本,它将通过相同的API网关再次开始工作。
跟踪流量,似乎调用了.html页所提供的三个XHR资源引起了问题。这些从我们的API网关返回401。它们返回401的原因是未传递cookie。
这三个调用是:
- https:// base_address / base_context / swagger-resources / configuration / ui
- https:// base_address / base_context / swagger-resources / configuration / security
- https:// base_address / base_context / swagger-resources
如果我将这些URL作为纯浏览器请求加载-它们可以工作-因为发送了cookie。
我怀疑是否适用CORS,因为HTML是从与招摇的JSON和实际服务调用相同的地址提供的。
知道为什么会这样吗?有人遇到过类似的问题吗?解决方法的建议?在此先感谢。
添加安全配置-跳过以下用于身份验证的URL ::
1 2 3 4 5 6 7 8 9 10 11 | 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); } |
在spring boot类中添加以下注释对我来说解决了这个问题。
@EnableSwagger2
我正在使用摇摇欲坠的版本
1 | <version>2.9.2</version> |
见下面的编辑
您是否使用Spring Security?
如果是,则可能会跳过这样的一些资源(对吗?):
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
尝试将其从
我对swagger的特定安全配置是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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; } |
如果您需要/想要,我可以将示例项目发送到GitHub,以了解有关我的安全性/高级配置的更多信息。
编辑2018/04/10
此问题是由springfox版本错误引起的。请在github上查看此问题以解决问题。
后代:
在pom.xml中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | ... <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> springfox-swagger2</artifactId> <version>2.8.1-SNAPSHOT</version> </dependency> <dependency> <groupId>io.springfox</groupId> springfox-swagger-ui</artifactId> <version>2.8.1-SNAPSHOT</version> </dependency> ... |
扩展WebSecurityConfigAdapter的类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @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)); } } } |
RestAuthenticationEntryPoint:
1 2 3 4 5 6 7 8 | @Component public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException { response.sendError(HttpServletResponse.SC_UNAUTHORIZED,"Unauthorized"); } } |
这发生在我身上,我使用的是SpringBoot 1.5.16和Springfox 2.9.1。
在我的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | @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/"); } } |
我正在访问http:// localhost:8080 / context / swagger-ui.html,但是使用该配置,正确的URL是:http:// localhost:8080 / context / api / swagger-ui.html
就我而言,问题的原因是:
1 | @ComponentScan(basePackageClasses = {ApplicationRoot.class }) |
在两个Java文件中两次。
删除多余的一个后,问题消失了。
将springfox-swagger2和springfox-swagger-ui依赖项升级到2.9.2,并确保已正确提供basePackage
1 2 3 4 5 | return new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors .basePackage("org.abc.xyz.controller")) .paths(PathSelectors.regex("/.*")) .build().apiInfo(apiEndPointsInfo()); |
我不使用spring安全发生了这个问题。我的项目使用Maven多个模块,当访问localhost:8080 / swagger-ui.html发生此问题时,首先在SwaggerConf类中添加@ EnableSwagger2,最后将@EnableSwagger移至SpringBoot Application类,此问题已解决。
第一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api(){ return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.zuoyan.")) .paths(PathSelectors.any()) .build(); } } |
最后:
1 2 3 4 5 6 7 8 | @SpringBootApplication(scanBasePackages = {"com.zuoyan.springboot.appmissionhall"}) @EnableSwagger2 public class ApplicationStartUpApplication { public static void main(String[] args) { SpringApplication.run(ApplicationStartUpApplication.class, args); } } |
首先确保添加了这两个依赖项,然后使用@ EnableSwagger2注释主SpringBootApplication类,然后将解决您的问题。
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>io.springfox</groupId> springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> |
在许多情况下,这是由于Java版本不兼容所致。很多时候它不适用于
通过将
1 2 3 4 5 6 7 8 9 10 | @SpringBootApplication @EnableSwagger2WebMvc @Import(SpringDataRestConfiguration.class) public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } |
这也可能是由
1 2 3 4 5 6 7 8 9 10 | <dependency> <groupId>io.springfox</groupId> springfox-swagger2</artifactId> <version>2.6.1</version> </dependency> <dependency> <groupId>io.springfox</groupId> springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> |
您需要确保
我在App类中添加了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.telixia.educare.academy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; @EnableSwagger2WebMvc @SpringBootApplication public class AcademyApplication { public static void main(String[] args) { SpringApplication.run(AcademyApplication.class, args); } } |
如果不指定任何特殊的组件扫描选项,则将带有@ EnableSwagger2批注的类放在Spring Boot Application类(@SpringBootApplication)的层次结构中的包中,将会遇到此问题。
假设您的Spring Boot Application类位于" de.oopexpert.app"中,然后将@ EnableSwagger2带注释的类放入...
- de.oopexpert.app将起作用
- de.oopexpert.app.config将起作用
- de.oopexpert.config将不起作用
您可以通过添加@ComponentScan(basePackages = {" de.oopexpert"})来调整组件扫描选项,以指定层次结构的其他根。
尝试使用8080端口-将其更改为8080后为我工作