繁体   English   中英

如何在 spring-boot 中完全禁用 swagger-ui?(/swagger-ui.html 应该返回 404)

[英]How to fully disable swagger-ui in spring-boot?(/swagger-ui.html should return 404)

我已阅读以下主题:使用 Spring MVC 禁用 Swagger

我写道:

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.project.name.controller"))
            .paths(PathSelectors.ant("/api/**"))
            .build()
            .apiInfo(apiInfo())
            .enable(false);
}

但是,如果我尝试访问 swagger ui: localhost:8080/swagger-ui.html
我懂了在此处输入图片说明

看起来不准确。 我可以完全禁用这个 URL 吗? 例如 404 或类似的东西。

我的答案与之前提供的答案相似,但略有不同。 我通常创建一个名为swagger的单独弹簧配置文件。 当我想启用 Swagger 时,我会在启动我的应用程序时传递以下 VM 标志-Dspring.profiles.active=swagger 这是我的 Swagger 配置的示例,

@Profile(value = {"swagger"})
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
    ...
}

下次当您尝试在没有swagger配置文件的情况下访问swagger-ui.html ,您将看到一个空的 Swagger 屏幕,而不是 404。

在此处输入图片说明

如果你根本不想加载静态 Swagger UI 页面,你可以编写一个简单的控制器,如下所示,

@Profile("!swagger")
@RestController
@Slf4j
public class DisableSwaggerUiController {

    @RequestMapping(value = "swagger-ui.html", method = RequestMethod.GET)
    public void getSwagger(HttpServletResponse httpResponse) throws IOException {
        httpResponse.setStatus(HttpStatus.NOT_FOUND.value());
    }
}

现在,如果您尝试在没有swagger配置文件的情况下访问swagger-ui.html ,您将得到 404。

使用 swagger 3.0.0 版本,您可以在相应的环境配置文件application.properties文件中添加springfox.documentation.enabled=false 例如,我已将此添加到application-prod.properties以在生产中禁用(在运行应用程序时,您必须使用 VM args 指定配置文件,如-Dspring.profiles.active=prod

您可以将@EnableSwagger2外部@EnableSwagger2它自己的@Configruation并通过属性或配置文件有条件地加载它。 例如

@Profile("!production")
@Configuration
@EnableSwagger2
public class SwaggerConfiguration{
    //Additional Swagger Beans

}

这将为任何非生产配置文件激活招摇。

如果您在控制器中没有 Swagger 注释......只需排除 SwaggerConfig.class 和 swagger 对构建的依赖

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>com/company/app/SwaggerConfig.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger-ui</artifactId>
                    </exclude>
                    <exclude>
                        <groupId>io.springfox</groupId>
                        <artifactId>springfox-swagger2</artifactId>
                    </exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

对于那些使用代码生成器的人:

@Controller
@Profile({"dev", "staging"})
public class HomeController {
    @RequestMapping(value = "/")
    public String index() {
        System.out.println("swagger-ui.html");
        return "redirect:swagger-ui.html";
    }
}

并将文件添加到您 .swagger-codegen-ignore 否则您的更改将在下一个 maven 构建中被覆盖

只需删除依赖项。

<!--<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
</dependency>-->

它不影响编译。

对于 SpringDoc 用户,将此添加到您的application.properties

springdoc.api-docs.enabled=false

要仅在prod配置文件处于活动状态时禁用 Swagger,请将其添加到您的application-prod.properties

添加到@Hayden 的答案(我没有足够的点数来评论..)

根据 springdoc 文档,您可以使用以下属性禁用 springdoc api 端点和 swagger-ui:

https://springdoc.org/#disabling-the-springdoc-openapi-endpoints

# Disabling the /v3/api-docs enpoint
springdoc.api-docs.enabled=false

https://springdoc.org/#disabling-the-swagger-ui

# Disabling the swagger-ui
springdoc.swagger-ui.enabled=false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM