Spring Security 无法推断基本URL,Swagger UI出错

tzcvj98z  于 2023-06-29  发布在  Spring
关注(0)|答案(1)|浏览(141)

我正在使用swagger做docs API,得到错误无法推断基本url,我尝试了一些以前的解决方案stackoverflow,但不工作,有人帮助我。我用的是swagger 2.7.0

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.7.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.7.0</version>
</dependency>

SwaggerConfig:

@EnableSwagger2
public class SwaggerConfig {
     @Bean
     public Docket apiDocket() {
         return new Docket(DocumentationType.SWAGGER_2)
                 .select()
                 .apis(RequestHandlerSelectors.basePackage("com.javaspring.blogapi"))
                 .paths(PathSelectors.any())
                 .build();
     }
}

安全配置:

http.authorizeHttpRequests(auth ->
                 Auth
                         .requestMatchers("/api/v1/auth/**",
                                 "/v2/api-docs",
                                 "/v3/api-docs",
                                 "/v3/api-docs/**",
                                 "/swagger-resources",
                                 "/swagger-resources/**",
                                 "/configuration/ui",
                                 "/configuration/security",
                                 "/swagger-ui/**",
                                 "/webjars/**",
                                 "/swagger-ui.html").permitAll()

其中一个控制器:

@RestController
@RequestMapping(path = "/users")
public class UserController {
     @Autowired
     private UserService userService;

     @GetMapping("/{username}")
     public ResponseEntity<UserDTO> getUser(@PathVariable String username){
         return ResponseEntity.ok().body(userService.findByUsername(username));
     }

我试着在输入中输入http://localhost:8080/,结果显示无法推断基本URL,但没有解决问题。
具体通知:无法推断基URL。这在使用动态servlet注册或API位于API网关之后时很常见。基本url是所有swagger资源被提供的根。例如,如果API在http://example.org/api/v2/api-docs上可用,则基本url是http://example.org/api/。请手动输入位置:

lp0sw83n

lp0sw83n1#

在swagger config中的build()之后添加.pathMapping("/")
并在www.example.com中添加此属性application.properties:

spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

相关问题