我正在开发基于SpringBoot的REST Api并使用Springfox生成swagger文档。
这是Swagger配置:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Autowired
private AppConfiguration appConfiguration;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("app-api")
.apiInfo(apiInfo())
.select()
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("App API")
.termsOfServiceUrl(appConfiguration.getApiTosUrl())
.version("1.0").build();
}
}
这是我正在使用的网络安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().authorizeRequests()
.antMatchers("/no-auth/**").permitAll()
.antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()
.anyRequest().authenticated()
.and()
.addFilter(new JWTAuthenticationFilter(securityConfiguration, authenticationManager()))
// this disables session creation on Spring Security
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", new CorsConfiguration().applyPermitDefaultValues());
return source;
}
访问网址: http://localhost:8080/swagger-ui.html 抛出HTTP错误405:不支持GET请求。
虽然我可以访问网址:http://localhost:8080/v2/api-docs?group=app-api
我已经在网上搜索了一个解决方案,但我不知道为什么会发生这种情况。
您可以告诉我如何解决此问题。
编辑: 我注意到swagger-ui.htm根本没有映射! 我使用的是springfox 2.8.0
2018-05-14 12:17:47.019 INFO 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping:已映射 “{[/ swagger-resources]}”上市 org.springframework.http.ResponseEntity> springfox.documentation.swagger.web.ApiResourceController.swaggerResources() 2018-05-14 12:17:47.020 INFO 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping:已映射 “{[/ swagger-resources / configuration / ui]}”上市 org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.uiConfiguration() 2018-05-14 12:17:47.021 INFO 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping:已映射 “{[/ swagger-resources / configuration / security]}”上市 org.springframework.http.ResponseEntity springfox.documentation.swagger.web.ApiResourceController.securityConfiguration() 2018-05-14 12:17:47.026 INFO 1012 --- [restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping:将“{[/ error]}”映射到 上市 org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
答案 0 :(得分:0)
当我有一个请求映射/ **时,我也有同样的感觉。 405的原因是Swagger请求由另一个requestmapping处理。您可以在DispatcherServlet中将其更改为宽大的URL。我在https://github.com/pmoerenhout/springfox-boot-bug做了一个最小的例子。
您可以使用此Spring映射来排除Swagger UI模式:
@RequestMapping(value = { "/", "/{part:^(?!swagger-ui$).*}/**" })